struts2--文件上傳和uploadFile攔截器

單文件上傳

1. 通過HTML表單上傳文件時, 需要將表單的enctype屬性設置爲multipart/form-data, method屬性設置爲post. 

jsp頁面代碼:

Html代碼  收藏代碼
  1. <form action="${pageContext.request.contextPath}/upload/uploadAction_saveFile.action"  
  2.       name="form1" method="post" enctype="multipart/form-data">  
  3.     上傳文件名稱: <input type="file" name="strutsUpload">  
  4.     <input type="submit" value="上傳">  
  5. </form>  

2. 在Action類中定義一下3個成員變量, 併爲其提供getter和setter方法:

Java代碼  收藏代碼
  1. private File strutsUpload; // 上傳的文件  
  2. private String strutsUploadContentType;// 文件的類型  
  3. private String strutsUploadFileName;// 文件的名稱  

以上3個成員變量的名稱不能隨意更改, private File strutsUpload變量的名稱必須和jsp中上傳文件標籤中的name屬性的值一致. 而private String strutsUploadContentType變量的名稱必須爲"上傳文件的名稱+ContentType", private String strutsUploadFileName變量的名稱必須爲"上傳文件的名稱+FileName".

3. 在Action類中定義業務方法. 完整的Action類可以如下:

Java代碼  收藏代碼
  1. public class UploadAction extends ActionSupport {  
  2.     private File strutsUpload; // 上傳的文件  
  3.     private String strutsUploadContentType;// 文件的類型  
  4.     private String strutsUploadFileName;// 文件的名稱  
  5.     // 業務方法  
  6.     public String saveFile() {  
  7.         try {  
  8.             ServletContext context = ServletActionContext.getServletContext();  
  9.             // 獲得當前web應用所在目錄下file文件夾的絕對路徑  
  10.             String path = context.getRealPath("/file");  
  11.             File destFile = new File(path, strutsUploadFileName);  
  12.             if (!destFile.exists()) {  
  13.                 destFile.createNewFile();  
  14.             }  
  15.             FileUtils.copyFile(strutsUpload, destFile);  
  16.         } catch (IOException e) {  
  17.             e.printStackTrace();  
  18.             throw new RuntimeException(e);  
  19.         }  
  20.         return "success";  
  21.     }  
  22.     // 省略getter和setter方法  
  23. }  

4. 在struts.xml文件中配置uploadFile攔截器的屬性:

Xml代碼  收藏代碼
  1. <package name="upload" namespace="/upload" extends="struts-default">  
  2.     <action name="uploadAction_*" class="cn.xing.upload.UploadAction" method="{1}">  
  3.         <interceptor-ref name="defaultStack">  
  4.             <!--  
  5.                 修改允許上傳文件的大小(默認值是2M),  
  6.                 將調用FileUploadInterceptor中的setMaximumSize(223434555)  
  7.             -->  
  8.             <param name="fileUpload.maximumSize">223434555</param>  
  9.             <!-- 配置允許上傳文件的類型,如果有多個類型用","隔開 -->  
  10.             <param name="fileUpload.allowedTypes">application/vnd.ms-excel,text/plain</param>  
  11.             <!--配置允許上傳文件的擴展名,如果有多個用","隔開  -->  
  12.             <param name="fileUpload.allowedExtensions">txt,excel,ppt</param>  
  13.         </interceptor-ref>  
  14.         <result name="success">/upload/success.jsp</result>  
  15.         <result name="input">/upload/error.jsp</result>  
  16.     </action>  
  17. </package>  

 

多文件上傳

多文件上傳與單文件上傳類似, 只有jsp表單和Action類的代碼有所不同.

1. jsp表單代碼:

Html代碼  收藏代碼
  1. <form action="${pageContext.request.contextPath}/upload/uploadsAction_saveFiles.action"  
  2.       name="form1" method="post" enctype="multipart/form-data">  
  3.     上傳文件名稱: <input type="file" name="strutsUploads"><br>  
  4.     上傳文件名稱: <input type="file" name="strutsUploads"><br>  
  5.     上傳文件名稱: <input type="file" name="strutsUploads"><br>  
  6.     <input type="submit" value="上傳">  
  7. </form>  

注意每個文件上傳標籤的name屬性需要一致.

2. Action類:

Java代碼  收藏代碼
  1. public class UploadsAction extends ActionSupport {  
  2.     private File[] strutsUploads;  
  3.     private String[] strutsUploadsContentType;  
  4.     private String[] strutsUploadsFileName;  
  5.     public String saveFiles() {  
  6.         ServletContext context = ServletActionContext.getServletContext();  
  7.         String realpath = context.getRealPath("/file");  
  8.         try {  
  9.             if (strutsUploads != null && strutsUploads.length > 0) {  
  10.                 for (int i = 0; i < strutsUploads.length; i++) {  
  11.                     File destFile = new File(realpath, strutsUploadsFileName[i]);  
  12.                     if (!destFile.exists()) {  
  13.                         destFile.createNewFile();  
  14.                     }  
  15.                     FileUtils.copyFile(strutsUploads[i], destFile);  
  16.                 }  
  17.             }  
  18.         } catch (IOException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.         return "success";  
  22.     }  
  23.     // 省略getter和setter方法  
  24. }  

多文件上傳時, Action中的3個成員變量的名稱需要遵循與單文件上傳時相同的規則. 此時3個成員變量均爲數組.

3. 在struts.xml文件中配置uploadFile攔截器的屬性, 同上.

 

錯誤顯示

當文件上傳過程中出錯時, 如果定義了錯誤顯示頁面, 將跳轉到指定的頁面, 並輸出錯誤信息.

1. 在action標籤下定義如下子標籤:

<!-- 定義上傳出錯要轉向的頁面 -->

<result name="input">/upload/error.jsp</result>

2. /upload/error.jsp頁面可以使用<s:fielderror/>標籤輸出錯誤信息.

前提是已經使用taglib指令導入了struts標籤庫:

<%@ taglib uri="/struts-tags" prefix="s"%>

3. 默認的錯誤信息爲英文, 在struts2-core-2.x.x.x.jar\org\apache\struts2\struts-messages.properties文件中定義:

struts.messages.error.uploading=Error uploading: {0}

struts.messages.error.file.too.large=File too large: {0} "{1}" "{2}" {3}

struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}

struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}

{0}:<input type=“file” name=“uploadImage”>中name屬性的值

{1}:上傳文件的名稱

{2}:上傳文件保存到臨時目錄的名稱

{3}:上傳文件的類型(對struts.messages.error.file.too.large是上傳文件的大小)

我們可以在Action的統計目錄下創建一個fileuploadmessage.properties文件, 文件名沒有要求, 但必須是properties文件, 在其中輸入:

struts.messages.error.uploading=上傳錯誤: {0}

struts.messages.error.file.too.large=文件太大: {0} "{1}" "{2}" {3}

struts.messages.error.content.type.not.allowed=不支持的文件類型: {0} "{1}" "{2}" {3}

struts.messages.error.file.extension.not.allowed=不支持的文件擴展名: {0} "{1}" "{2}" {3}

使用jdk目錄下的native2ascii工具將中文轉爲unicode編碼.

接下來需要在struts.xml文件中加載自定義的資源文件:

<constant name="struts.custom.i18n.resources" value="cn.xing.upload.fileuploadmessage"></constant>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章