标签:
1、单文件上传
单文件上传步骤:
1)创建上传jsp页面
文件上传的表单提交方式必须是POST方式,编码类型:enctype="multipart/form-data",默认是 application/x-www-form-urlencoded,比如:
1 <form action="${pageContext.servletContext.contextPath}/uploadAction.action" enctype="multipart/form-data" method="post">
2)创建处理文件上传Action类
3)配置struts.xml文件
1 <constant name="struts.multipart.maxSize" value="20000000"></constant>
设置上传文件总大小,对所有上传form有效,只想对当前form进行设置,可以设置fileUpload拦截器属性
可以在struts.xml文件中配置这些属性:
1 <interceptor-ref name="defaultStack"> 2 <!--配置上传文件的大小,这里配置的是上传文件的单个大小--> 3 <param name="fileUpload.maximumSize">20971520</param> 4 <!-- 配置上传文件允许的类型--> 5 <param name="fileUpload.allowedTypes">text/plain,application/msword</param> 6 <!-- 配置上传文件的扩展名--> 7 <param name="fileUpload.allowedExtensions">.txt,.doc</param> 8 </interceptor-ref>
允许的类型(allowedTypes)和允许的扩展名(allowedExtensions)必须保持一致。
单文件上传示例:
1)创建jsp页面
1 <body> 2 <form action="${pageContext.servletContext.contextPath}/uploadAction.action" enctype="multipart/form-data" method="post"> 3 上传文件:<input type="file" name="upload"> 4 <input type="submit" value="上传"> 5 </form> 6 </body>
2)创建Action类
1 public class UploadAction extends ActionSupport { 2 private File upload; 3 private String uploadContentType; 4 private String uploadFileName; 5 6 public File getUpload() { 7 return upload; 8 } 9 10 public void setUpload(File upload) { 11 this.upload = upload; 12 } 13 14 public String getUploadContentType() { 15 return uploadContentType; 16 } 17 18 public void setUploadContentType(String uploadContentType) { 19 this.uploadContentType = uploadContentType; 20 } 21 22 public String getUploadFileName() { 23 return uploadFileName; 24 } 25 26 public void setUploadFileName(String uploadFileName) { 27 this.uploadFileName = uploadFileName; 28 } 29 30 @Override 31 public String execute() throws Exception { 32 ServletContext sc = ServletActionContext.getServletContext(); 33 //获取要保存文件的位置 34 String path = sc.getRealPath("/upload"); 35 //创建一个与上传文件同名的文件 36 File file = new File(path, uploadFileName); 37 //将临时文件内容拷贝到目标文件夹下的那个同名的文件 38 FileUtils.copyFile(upload, file); 39 //删除临时文件 40 upload.delete(); 41 return SUCCESS; 42 } 43 }
3)配置struts.xml文件
1 <struts> 2 <constant name="struts.devMode" value="true" /> 3 <constant name="struts.multipart.maxSize" value="200000000"/> 4 <package name="default" namespace="/" extends="struts-default"> 5 <action name="uploadAction" class="com.sunny.action.UploadAction"> 6 <result>/success.jsp</result> 7 <result name="input">/error.jsp</result> 8 <interceptor-ref name="defaultStack"> 9 <param name="fileUpload.maximumSize">20971520</param> 10 <param name="fileUpload.allowedTypes">text/plain,application/msword</param> 11 <param name="fileUpload.allowedExtensions">.txt,.doc</param> 12 </interceptor-ref> 13 </action> 14 </package> 15 </struts>
4)上传文件界面
5)上传成功后会跳转到success.jsp页面
6)项目下的upload文件夹下会有上传的文件
2、多文件上传
多文件上传的所有流程与单文件上传一致,但是需要注意的是:
单文件上传示例:
1)创建jsp页面
1 <body> 2 <form action="${pageContext.servletContext.contextPath}/uploadAction.action" enctype="multipart/form-data" method="post"> 3 上传文件:<input type="file" name="upload"><br> 4 上传文件:<input type="file" name="upload"><br> 5 上传文件:<input type="file" name="upload"><br> 6 上传文件:<input type="file" name="upload"><br> 7 <input type="submit" value="上传"> 8 </form> 9 </body>
2)创建Action类
1 public class UploadAction extends ActionSupport { 2 private File[] upload; 3 private String[] uploadContentType; 4 private String[] uploadFileName; 5 6 public File[] getUpload() { 7 return upload; 8 } 9 10 public void setUpload(File[] upload) { 11 this.upload = upload; 12 } 13 14 public String[] getUploadContentType() { 15 return uploadContentType; 16 } 17 18 public void setUploadContentType(String[] uploadContentType) { 19 this.uploadContentType = uploadContentType; 20 } 21 22 public String[] getUploadFileName() { 23 return uploadFileName; 24 } 25 26 public void setUploadFileName(String[] uploadFileName) { 27 this.uploadFileName = uploadFileName; 28 } 29 30 @Override 31 public String execute() throws Exception { 32 ServletContext sc = ServletActionContext.getServletContext(); 33 //获取要保存文件的位置 34 String path = sc.getRealPath("/upload"); 35 //创建一个与上传文件同名的文件 36 for (int i = 0; i < upload.length; i++) { 37 File file = new File(path, uploadFileName[i]); 38 //将临时文件内容拷贝到目标文件夹下的那个同名的文件 39 FileUtils.copyFile(upload[i], file); 40 //删除临时文件 41 upload[i].delete(); 42 } 43 return SUCCESS; 44 } 45 }
3)配置struts.xml文件,和单文件上传的配置一样
1 <struts> 2 <constant name="struts.devMode" value="true" /> 3 <constant name="struts.multipart.maxSize" value="200000000"/> 4 <package name="default" namespace="/" extends="struts-default"> 5 <action name="uploadAction" class="com.sunny.action.UploadAction"> 6 <result>/success.jsp</result> 7 <result name="input">/error.jsp</result> 8 <interceptor-ref name="defaultStack"> 9 <param name="fileUpload.maximumSize">20971520</param> 10 <param name="fileUpload.allowedTypes">text/plain,application/msword</param> 11 <param name="fileUpload.allowedExtensions">.txt,.doc</param> 12 </interceptor-ref> 13 </action> 14 </package> 15 </struts>
4)上传文件界面
5)上传成功后会跳转到success.jsp页面
6)项目下的upload文件夹下会有上传的文件
标签:
原文地址:http://www.cnblogs.com/xiaolongSunny/p/4661336.html