struts2-文件上传2(多文件)
File Upload 拦截器
FileUpload 拦截器负责处理文件的上传操作, 它是默认的 defaultStack 拦截器栈的一员.
FileUpload 拦截器有 3 个属性可以设置.
•maximumSize: 上传文件的最大长度(以字节为单位), 默认值为 2 MB
•allowedTypes: 允许上传文件的类型, 各类型之间以逗号分隔
•allowedExtensions: 允许上传文件扩展名, 各扩展名之间以逗号分隔
可以在 struts.xml 文件中覆盖这 3 个属性:
1 <interceptor-ref name="defaultStack"> 2 <!-- 3 配置文件上传拦截器的参数 4 * 与定义参数的顺序无关 5 * 允许的类型(allowedTypes)和允许的扩展名(allowedExtensions)必须保持一致,要同时存在 6 --> 7 <!-- 8 * 配置上传文件的大小 9 * struts.xml文件中配置的是上传文件的总大小 10 * 这里配置的是上传文件的单个大小 11 --> 12 <param name="fileUpload.maximumSize">20971520</param> 13 <!-- 配置上传文件允许的类型,如果配置多个值的话,用","隔开 --> 14 <param name="fileUpload.allowedTypes">text/plain,application/msword</param> 15 <!-- 配置上传文件的扩展名,如果配置多个值的话,用","隔开 --> 16 <param name="fileUpload.allowedExtensions">.txt,doc</param> 17 </interceptor-ref>
若用户上传的文件大小大于给定的最大长度或其内容类型没有被列在 allowedTypes, allowedExtensions 参数里, 将会显示一条出错消息. 与文件上传有关的出错消息在
struts-messages.properties 文件里预定义. (org.apache.struts2包下)
可以在文件上传 Action 相对应的资源文件中重新定义错误消息, 但需要在 struts.xml 文件中配置使用 action 的消息:
1 扩展名不允许: uploadImages ".eclipseproduct" "upload__e9b1c30_13eb1781bf8__7ff8_00000005.tmp" application/octet-stream
查看struts-messages.properties文件
1 配置如下: 2 struts.messages.error.uploading=Error uploading: {0} 3 struts.messages.error.file.too.large=File too large: {0} "{1}" "{2}" {3} 4 struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3} 5 struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3} 6 {0}:<input type=“file” name=“uploadImage”>中name属性的值 7 {1}:上传文件的名称 8 {2}:上传文件保存到临时目录的名称 9 {3}:上传文件的类型(对struts.messages.error.file.too.large是上传文件的大小)
修改显示错误的资源文件的信息
1 第一步:创建新的资源文件 例如fileuploadmessage.properties,放置在src下 2 在该资源文件中增加如下信息 3 struts.messages.error.uploading=上传错误: {0} 4 struts.messages.error.file.too.large=上传文件太大: {0} "{1}" "{2}" {3} 5 struts.messages.error.content.type.not.allowed=上传文件的类型不允许: {0} "{1}" "{2}" {3} 6 struts.messages.error.file.extension.not.allowed=上传文件的后缀名不允许: {0} "{1}" "{2}" {3} 7 8 第二步:在struts.xml文件加载该资源文件 9 10 <!-- 配置上传文件的出错信息的资源文件 --> 11 <constant name="struts.custom.i18n.resources" value=“cn….xxx.fileuploadmessage“/>
多文件上传代码
1 第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。 2 第二步:把form表的enctype设置为:“multipart/form-data“,如下: 3 <form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post"> 4 <input type="file" name="uploadImages"> 5 <input type="file" name="uploadImages"> 6 </form> 7 第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称: 8 public class uploadAction{ 9 private File[] uploadImages;//得到上传的文件 10 private String[] uploadImagesContentType;//得到文件的类型 11 private String[] uploadImagesFileName;//得到文件的名称 12 //这里略省了属性的getter/setter方法 13 public String saveFiles() throws Exception{ 14 ServletContext sc = ServletActionContext.getServletContext(); 15 String realpath = sc.getRealPath("/uploadfile"); 16 try { 17 if(uploadImages!=null&&uploadImages.length>0){ 18 for(int i=0;i<uploadImages.length;i++){ 19 File destFile = new File(realpath,uploadImageFileNames[i]); 20 FileUtils.copyFile(uploadImages[i], destFile); 21 } 22 } 23 } catch (IOException e) { 24 e.printStackTrace();}return "success";}
25 }
struts2框架上传多文件总结:
1 多文件上传: 2 * 所有流程于配置都与单文件上传一致。 3 * 需要注意的是: 4 * 在页面中,虽然是多文件上传,但是页面中表单的name属性的值必须保持一致; 5 * 在动作类action中声明的相关属性,类型改成数组; 6 * 在业务方法中,相关处理流程改成单文件上传的循环。
1、jsp
uploads.jsp
1 <%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%> 2 <%@ taglib uri="/struts-tags" prefix="s"%> 3 <html> 4 <head> 5 <title>My JSP 'index.jsp' starting page</title> 6 </head> 7 <body> 8 <form action="${pageContext.request.contextPath}/upload/uploadsAction_saveFiles.action" 9 name="form1" method="post" enctype="multipart/form-data" > 10 上传文件名称:<input type="file" name="uploadImages"><br> 11 上传文件名称:<input type="file" name="uploadImages"><br> 12 上传文件名称:<input type="file" name="uploadImages"><br> 13 14 <input type="submit" value="上传"> 15 </form> 16 </body> 17 </html>
2、java
UploadsAction.java
1 import java.io.File; 2 import java.io.IOException; 3 4 import javax.servlet.ServletContext; 5 6 import org.apache.commons.io.FileUtils; 7 import org.apache.struts2.ServletActionContext; 8 9 import com.opensymphony.xwork2.ActionSupport; 10 11 @SuppressWarnings("serial") 12 public class UploadsAction extends ActionSupport { 13 14 private File [] uploadImages; 15 16 private String [] uploadImagesContentType; 17 18 private String [] uploadImagesFileName; 19 20 public String saveFiles(){ 21 System.out.println("UploadsAction ******** saveFiles()"); 22 23 ServletContext sc = ServletActionContext.getServletContext(); 24 25 String path = sc.getRealPath("/fileupload"); 26 27 for(int i=0;i<uploadImages.length;i++){ 28 File file = new File(path, uploadImagesFileName[i]); 29 30 try { 31 FileUtils.copyFile(uploadImages[i], file); 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } 35 36 uploadImages[i].delete(); 37 38 } 39 40 return "success"; 41 } 42 43 public File[] getUploadImages() { 44 return uploadImages; 45 } 46 47 public void setUploadImages(File[] uploadImages) { 48 this.uploadImages = uploadImages; 49 } 50 51 public String[] getUploadImagesContentType() { 52 return uploadImagesContentType; 53 } 54 55 public void setUploadImagesContentType(String[] uploadImagesContentType) { 56 this.uploadImagesContentType = uploadImagesContentType; 57 } 58 59 public String[] getUploadImagesFileName() { 60 return uploadImagesFileName; 61 } 62 63 public void setUploadImagesFileName(String[] uploadImagesFileName) { 64 this.uploadImagesFileName = uploadImagesFileName; 65 } 66 67 }
3、xml
struts_upload.xml
1 <action name="uploadsAction_saveFiles" class="cn.zengfansheng.struts.upload.UploadsAction" method="uploads"> 2 <result name="success">/upload/success.jsp</result> 3 <interceptor-ref name="defaultStack" > 4 <param name="fileUpload.maximumSize">20971520</param> 5 <param name="fileUpload.allowedTypes">text/plain,application/msword,image/jpeg,application/octet-stream,text/xml</param> 6 <param name="fileUpload.allowedExtensions">.txt,doc,jpg,exe,xml</param> 7 </interceptor-ref> 8 <result name="input">/upload/error.jsp</result> 9 </action>
struts.xml
1 <constant name="struts.custom.i18n.resources" 2 value="cn.zengfansheng.struts.upload.fileuploadmessage"> 3 <!-- 省略后缀名,如果配置了多个struts.custom.i18n.resources,后面的会覆盖前面的,出现不起效果 --> 4 </constant> 5 <include file="cn/zengfansheng/struts/upload/struts_upload.xml"></include>
4、properties
fileuploadmessage.properties
1 struts.messages.error.uploading=\u4E0A\u4F20\u51FA\u9519\: {0} 2 struts.messages.error.file.too.large=\u6587\u4EF6\u8FC7\u5927\: {0} "{1}" "{2}" {3} 3 struts.messages.error.content.type.not.allowed=\u7C7B\u578B\u4E0D\u5141\u8BB8\: {0} "{1}" "{2}" {3} 4 struts.messages.error.file.extension.not.allowed=\u6269\u5C55\u540D\u4E0D\u5141\u8BB8\: {0} "{1}" "{2}" {3}
by hacket