(1) 基于表单的文件上传
在建立表单是,不要忘了使用enctype属性,并将它的值指定为multipart/form-date。表单enctype属性的缺省值是application/x-www-form-urlencoded,这种编码方案使用有限的字符集,当使用了非字母和数字的字符时,必须使用“%HH”代替(这里的H表示的十六进制数字)。
表单的method为post。
(2) 文件上传内容分析
对于文件上传来说,要读取文件的内容,不能使用ServletRequest接口的getParameter()方法,而需要调用ServletRequest接口的geInputSlream()方法来得到输入流,然后从输入流中读取传送的内容,再根据文件上传的格式进行分析,取出上传文件的内容和衣单中其他字段的内容。
(3) Struts 2对文件上传的支持
Ⅰ:需要的JAR包:
Ø commons-fileupload-1.2.1.jar
Ø commons-io-1.3.2.jar
Ⅱ:Struts 2提供了一个文档上传的拦截器:org.apache.struts2.interceptor.FileUploadInterceptor。他负责调用底层的文件上传组件解析文件内容,并为Action准备与上传文件相关的属性值。处理文件请求的Action必须提供特殊式命名的属性。例如,若表单中文件选择框<input tepy=”file” name=”p_w_picpath”>
上传单个文件:
UpfileAction.java
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.util.Date;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class UpfileAction extends ActionSupport {
- private File file;
- private String fileFileName;
- private String fileContentType;
- private String description;
- //省略getter与setter方法
- @Override
- public String execute() throws Exception {
- String newFileName=null;
- long time =new Date().getTime(); //时间
- String path= ServletActionContext.getServletContext().getRealPath("upfile");//保存上传文件的目录
- File dir= new File(path); //新建目录
- if(!dir.exists()){
- dir.mkdir();
- }
- int index=fileFileName.lastIndexOf(".");
- if (index ==-1) {
- newFileName=fileFileName.substring(0,index)+"-"+ time; //文件名
- } else {
- newFileName =fileFileName.substring(0,index)+"-"+time+fileFileName.substring(index);
- }
- BufferedInputStream bin=null;
- BufferedOutputStream bout=null;
- try {
- FileInputStream fin =new FileInputStream(file);
- bin=new BufferedInputStream(fin);
- FileOutputStream fout =new FileOutputStream(new File(dir, newFileName));
- bout=new BufferedOutputStream(fout);
- byte[] b=new byte[1024];
- int len=0;
- while ((len=bin.read(b))!=-1) {
- bout.write(b, 0, len);
- }
- } catch (Exception e) {
- System.out.println("写出数据失败!!!!");
- }finally{
- try {
- bout.close();
- bin.close();
- } catch (Exception e2) {
- System.out.println("关闭数据流失败!!!!");
- }
- }
- return SUCCESS;
- }
- }
upfile.jsp:
- <body>
- <s:form action="upfile" method="post" enctype="multipart/form-data" >
- <s:file name="file" label="选择上传的文件"></s:file><br>
- <s:textfield name="description" label="文件描述"></s:textfield>
- <s:submit value="上传"></s:submit>
- </s:form>
- </body>
success.jsp:
- <body>
- <center>
- <h1>
- <font color="red">
- 文件名:<s:property value="fileFileName"/><br>
- 文件类型:<s:property value="fileContentType"/><br>
- 文件描述:<s:property value="description"/><br>
- </font>
- </h1>
- </center>
- </body>
struts.xml:
- <!-- 设置上传文件的最大长度 -->
- <constant name="struts.multipart.maxSize" value="102400000"></constant>
- <package name="upfile" extends="struts-default">
- <action name="upfile" class="com.mzsx.upfile.UpfileAction">
- <result name="success">/s16/success.jsp</result>
- <result name="input">/s16/upfile.jsp</result>
- </action>
- </package>
多文件上传
UpfileListAction.java
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.util.Date;
- import java.util.List;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class UpfileListAction extends ActionSupport {
- private List<File> file;
- private List<String> fileFileName;
- private List<String> fileContentType;
- private List<String> description;
- //省略getter与setter方法
- @Override
- public String execute() throws Exception {
- String newFileName=null;
- long time =new Date().getTime(); //时间
- String path= ServletActionContext.getServletContext().getRealPath("upfile");//保存上传文件的目录
- File dir= new File(path); //新建目录
- if(!dir.exists()){
- dir.mkdir();
- }
- for (int i=0;i<file.size();i++) {
- int index=fileFileName.get(i).lastIndexOf(".");
- if (index ==-1) {
- newFileName=fileFileName.get(i).substring(0,index)+"-"+ time; //文件名
- } else {
- newFileName =fileFileName.get(i).substring(0,index)+"-"+time+fileFileName.get(i).substring(index);
- }
- BufferedInputStream bin=null;
- BufferedOutputStream bout=null;
- try {
- FileInputStream fin =new FileInputStream(file.get(i));
- bin=new BufferedInputStream(fin);
- FileOutputStream fout =new FileOutputStream(new File(dir, newFileName));
- bout=new BufferedOutputStream(fout);
- byte[] b=new byte[1024];
- int len=-1;
- while ((len=bin.read(b))!=-1) {
- bout.write(b, 0, len);
- }
- } catch (Exception e) {
- System.out.println("写出数据失败!!!!");
- }finally{
- try {
- bout.close();
- bin.close();
- } catch (Exception e2) {
- System.out.println("关闭数据流失败!!!!");
- }
- }
- }
- return SUCCESS;
- }
- }
upfile.jsp:
- <body>
- <s:form action="upfilelist" method="post" enctype="multipart/form-data" >
- <s:file name="file" label="选择上传的文件"></s:file><br>
- <s:textfield name="description" label="文件描述"></s:textfield>
- <s:file name="file" label="选择上传的文件"></s:file><br>
- <s:textfield name="description" label="文件描述"></s:textfield>
- <s:file name="file" label="选择上传的文件"></s:file><br>
- <s:textfield name="description" label="文件描述"></s:textfield>
- <s:submit value="上传"></s:submit>
- </s:form>
- </body>
- 注意:所有file同名。
struts.xml
- <!-- 设置上传文件的最大长度 -->
- <constant name="struts.multipart.maxSize" value="102400000"></constant>
- <package name="upfilelist" extends="struts-default">
- <action name="upfilelist" class="com.mzsx.upfile.UpfileListAction">
- <result name="success">/s16/successList.jsp</result>
- <result name="input">/s16/upfileList.jsp</result>
- </action>
- </package>