Struts 2 <s:file>标签上传GB级别文件或多文件
2014-01-15 14:53
357 查看
struts.xml配置:
对于文件类型的过滤,优先级allowedTypes大于allowedExtensions,2种方法都可行,后者比较方便,配置了后者最好就不要使用前者了,因为优先级的原因会致其无效。
JSP页面:
action方法:
最后,上传过程中,可能会遇到read time out错误。原因是web容器的配置问题,以tomcat为例,需要修改tomcat的server.xml
<constant name="struts.multipart.maxSize" value="4096000000" />上面这个全局配置,也需要设置,默认是2M,必须也改成允许上传的文件大小
对于文件类型的过滤,优先级allowedTypes大于allowedExtensions,2种方法都可行,后者比较方便,配置了后者最好就不要使用前者了,因为优先级的原因会致其无效。
<!-- Application Library Manage Start --> <action name="uploadactions" class="appLibraryAction" method="upload"> <!-- 配置fileUpload的拦截器 --> <interceptor-ref name="fileUpload"> <!-- 配置允许上传的文件类型 --> <!-- 允许后缀名为png,bmp,jpg,doc,xls的文件上传 --> <param name="allowedExtensions"> png,bmp,jpg,ipa,apk,mkv,exe,zip,rar,iso </param> <!-- <param name="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg,image/pjpeg,image/jpg,application/msword,text/plain </param> --> <!-- 配置允许上传的文件大小 --> <!-- <param name="maximumSize">2000000000</param> --> </interceptor-ref> <interceptor-ref name="defaultStack" /> <result>/taguser/fileuploadoutput.jsp</result> <result name="input">/taguser/fileuploads.jsp</result> </action>
JSP页面:
<s:form action="uploadactions" method="post" enctype="multipart/form-data"> <s:file name="upload" label="path"/> <s:file name="upload" label="path"/> <s:file name="upload" label="path"/> <s:submit value="upload"/> </s:form>
action方法:
//封装多个上传文件域的属性 private List<File> upload = new ArrayList<File>(); // 封装多个上传文件类型的属性 private List<String> uploadContentType = new ArrayList<String>(); // 封装多个上传文件名的属性 private List<String> uploadFileName = new ArrayList<String>(); //动态设置上传文件保存地址 private String savePath; public String getSavePath() { System.out.println("getSavePath()!!!!!"); System.out.println(savePath+"++++++++++++++++++++++++++++++"); return savePath; } public void setSavePath(String savePath) { System.out.println("setSavePath()!!!!!"); this.savePath = savePath; // savePath = "E:\\butone\\struts2.2\\WebRoot\\images\\"+getUploadFileName(); } //上传多个文件对应文件内容的setter和getter方法 public List<File> getUpload() { return upload; } public void setUpload(List<File> upload) { System.out.println("---------------- setUpload(List<File> upload) ----------------"); this.upload = upload; } // 上传多个文件的文件类型setter和getter方法 public List<String> getUploadContentType() { return uploadContentType; } public void setUploadContentType(List<String> uploadContentType) { this.uploadContentType = uploadContentType; } // 上传多个文件的文件名的setter和getter方法 public List<String> getUploadFileName() { return uploadFileName; } public void setUploadFileName(List<String> uploadFileName) { this.uploadFileName = uploadFileName; } public String upload() { //savePath = "E:\\butone\\struts2.2\\WebRoot\\images\\"; savePath = ServletActionContext.getRequest().getRealPath(""); System.out.println("upload()!!!!!"); //上传多个文件 List<File> files = getUpload(); // String ext =""; FileOutputStream fos = null; FileInputStream fis = null; byte[] buffer = new byte[1024]; int len = 0; Random rd = new Random(); System.out.println(files.size()+" ----------------"); System.out.println(getSavePath()); for (int i = 0; i < files.size(); i++) { try { //以服务器的文件保存地址和当前时间组合文件名建立上传文件输出流 // ext =uploadFileName.get(i).substring(uploadFileName.get(i).lastIndexOf('.')); /* fos = new FileOutputStream(getSavePath()+ File.separator+ * DateFormatUtil.getCurrentCustomFormatDateTime(DateFormatUtil.DATE_TIME_FORMAT_14) + * String.valueOf(rd.nextInt(1000))+ext); */ System.out.println(getSavePath()+"------------------------getsavepath!!!"); fos = new FileOutputStream(getSavePath() + File.separator + uploadFileName.get(i)); // 以上传文件建立一个文件上传流 fis = new FileInputStream(files.get(i)); // 将上传文件的内容写入服务器 len = 0; while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } } catch (Exception e) { e.printStackTrace(); } } return SUCCESS; }
最后,上传过程中,可能会遇到read time out错误。原因是web容器的配置问题,以tomcat为例,需要修改tomcat的server.xml
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" disableUploadTimeout="false"/> <!--这里就添加了 disableUploadTimeout="false"一般默认情况下disableUploadTimeout的值为true-->
相关文章推荐
- ajax图片上传及时回显图片,自己总结 + ajaxFileUpload 上传文件 以及 返回值 带 <pre> 标签问题
- Struts2-文件上传-<s:file>标签使用
- ajaxFileUpload 上传文件 以及 返回值 带 <pre> 标签问题
- 使用input type=file标签进行文件上传时,在安卓手机中的微信浏览器中不能调起相机,但是在苹果手机中的微信浏览器中可以调用相机。解决办法很简单, <input type="file" nam
- 更改上传文件的样式<input type="file>
- PrimeFaces <p:fileUpload>上传文件 中文乱码
- 解决<s:file>上传文件大小和 不能获取文件问题
- <input id="File_Audio" type="file" /> 一步到位上传图片、音频等文件
- 选择多个文件上传<input type="file" id="myfileinput" multiple>
- WebView 加载H5 <Input type="file"/>标签上传图片问题
- js 实现 <input type="file" /> 文件上传
- 解决微信内嵌浏览器无法响应<input type='file'>上传文件的思路
- 将通过<input type="file">上传的txt文件存储在localStorage,提取并构建File对象
- 通过<input type=“file”>上传文件问题
- 用jsp<html:file>实现一个文件上传的例子,而且有验证
- 解决微信内嵌浏览器无法响应<input type='file'>上传文件的思路
- Ajax 上传文件返回JSON字符串FireFox解析出来后带有<pre>标签解决办法
- struts2中的配置文件的<constant>标签(struts.xml自动加载)
- Ajax 上传文件,返回JSON字符串在FireFox下解析带有<pre>标签的解决办法
- ASP------<input type="file"/>上传文件