單文件上傳
1. 通過HTML表單上傳文件時, 需要將表單的enctype屬性設置爲multipart/form-data, method屬性設置爲post.
jsp頁面代碼:
- <form action="${pageContext.request.contextPath}/upload/uploadAction_saveFile.action"
- name="form1" method="post" enctype="multipart/form-data">
- 上傳文件名稱: <input type="file" name="strutsUpload">
- <input type="submit" value="上傳">
- </form>
2. 在Action類中定義一下3個成員變量, 併爲其提供getter和setter方法:
- private File strutsUpload; // 上傳的文件
- private String strutsUploadContentType;// 文件的類型
- private String strutsUploadFileName;// 文件的名稱
以上3個成員變量的名稱不能隨意更改, private File strutsUpload變量的名稱必須和jsp中上傳文件標籤中的name屬性的值一致. 而private String strutsUploadContentType變量的名稱必須爲"上傳文件的名稱+ContentType", private String strutsUploadFileName變量的名稱必須爲"上傳文件的名稱+FileName".
3. 在Action類中定義業務方法. 完整的Action類可以如下:
- public class UploadAction extends ActionSupport {
- private File strutsUpload; // 上傳的文件
- private String strutsUploadContentType;// 文件的類型
- private String strutsUploadFileName;// 文件的名稱
- // 業務方法
- public String saveFile() {
- try {
- ServletContext context = ServletActionContext.getServletContext();
- // 獲得當前web應用所在目錄下file文件夾的絕對路徑
- String path = context.getRealPath("/file");
- File destFile = new File(path, strutsUploadFileName);
- if (!destFile.exists()) {
- destFile.createNewFile();
- }
- FileUtils.copyFile(strutsUpload, destFile);
- } catch (IOException e) {
- e.printStackTrace();
- throw new RuntimeException(e);
- }
- return "success";
- }
- // 省略getter和setter方法
- }
4. 在struts.xml文件中配置uploadFile攔截器的屬性:
- <package name="upload" namespace="/upload" extends="struts-default">
- <action name="uploadAction_*" class="cn.xing.upload.UploadAction" method="{1}">
- <interceptor-ref name="defaultStack">
- <!--
- 修改允許上傳文件的大小(默認值是2M),
- 將調用FileUploadInterceptor中的setMaximumSize(223434555)
- -->
- <param name="fileUpload.maximumSize">223434555</param>
- <!-- 配置允許上傳文件的類型,如果有多個類型用","隔開 -->
- <param name="fileUpload.allowedTypes">application/vnd.ms-excel,text/plain</param>
- <!--配置允許上傳文件的擴展名,如果有多個用","隔開 -->
- <param name="fileUpload.allowedExtensions">txt,excel,ppt</param>
- </interceptor-ref>
- <result name="success">/upload/success.jsp</result>
- <result name="input">/upload/error.jsp</result>
- </action>
- </package>
多文件上傳
多文件上傳與單文件上傳類似, 只有jsp表單和Action類的代碼有所不同.
1. jsp表單代碼:
- <form action="${pageContext.request.contextPath}/upload/uploadsAction_saveFiles.action"
- name="form1" method="post" enctype="multipart/form-data">
- 上傳文件名稱: <input type="file" name="strutsUploads"><br>
- 上傳文件名稱: <input type="file" name="strutsUploads"><br>
- 上傳文件名稱: <input type="file" name="strutsUploads"><br>
- <input type="submit" value="上傳">
- </form>
注意每個文件上傳標籤的name屬性需要一致.
2. Action類:
- public class UploadsAction extends ActionSupport {
- private File[] strutsUploads;
- private String[] strutsUploadsContentType;
- private String[] strutsUploadsFileName;
- public String saveFiles() {
- ServletContext context = ServletActionContext.getServletContext();
- String realpath = context.getRealPath("/file");
- try {
- if (strutsUploads != null && strutsUploads.length > 0) {
- for (int i = 0; i < strutsUploads.length; i++) {
- File destFile = new File(realpath, strutsUploadsFileName[i]);
- if (!destFile.exists()) {
- destFile.createNewFile();
- }
- FileUtils.copyFile(strutsUploads[i], destFile);
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return "success";
- }
- // 省略getter和setter方法
- }
多文件上傳時, 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>