Struts 2 標籤上傳GB級別檔案或多檔案
阿新 • • 發佈:2019-02-17
struts.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-->