標籤:
•Commons-FileUpload組件–Commons是Apache開放原始碼組織的一個Java子項目,其中的FileUpload是用來處理HTTP檔案上傳的子項目 •Commons-FileUpload組件特點–使用簡單:可以方便地嵌入到JSP檔案中,編寫少量代碼即可完成檔案的上傳功能–能夠全程式控制制上傳內容–能夠對上傳檔案的大小、類型進行控制 •需要下載Common-FileUplaod架構地址(當然MyEclipce中Struts2支援裡內建有這兩個包):–http://jakarta.apache.org/commons/fileupload/–下載commons-fileupload-1.2-bin.zip檔案–解壓後得到commons-fileupload-1.2.jar –http://jakarta.apache.org/commons/io/–下載commons-io-1.3.2-bin.zip檔案–解壓後得到commons-io-1.3.2.jar 基於表單的檔案上傳 •注意:上面是基於表單的上傳,在form標籤中有一個必須的屬性值設定:enctype. •如果要做檔案上傳功能必須將enctype值設為multipart/form-data. •此外,檔案上傳還需要二個JAR檔案支援:
檔案上傳頁面upload.jsp(二選一表單即可)
<%@ taglib uri="/struts-tags" prefix="s"%> <body> <form action="uploads" method="post" enctype="multipart/form-data"> 檔案: <input type="file" name="uploadImage"> <input type="submit" value="提交"> </form> 或者(兩個表單二選一即可) <s:form action="uploads" method="post" enctype="multipart/form-data"> <s:file name=“uploadImage " label="選擇上傳的檔案" /> <s:submit value="上傳" /> </s:form> </body>
Struts2.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts> <!-- 要注意Struts2預設檔案上傳最大為2M即便你設定了<param name="maximumSize">5242880</param> 當上傳的檔案大於2M時候也會出錯的這時要設定,另外一個常量 <constant name="struts.multipart.maxSize" value="1000000000"/> --> <package name="Struts2" extends="struts-default" namespace="/"> <action name="uploads" class="com.struts.UploadAction"> <!-- 指定(限制)上傳檔案的類型,定義局部攔截器,修改預設攔截器的屬性 "fileUpload.maximumSize" :限制上傳最大的檔案大小。 "fileUpload.allowedTypes":允許上傳檔案的類型。 "fileUpload.allowedExtensions":允許上傳檔案的可擴充檔案類型。 --> <interceptor-ref name="defaultStack"> <param name="fileUpload.maximumSize">500000000</param> <param name="fileUpload.allowedTypes">text/plain,application/vnd.ms-powerpoint</param> <param name="fileUpload.allowedExtensions">.txt,.ppt</param> </interceptor-ref> <result>/success.jsp</result> <!-- 出現錯誤自動會返回input結果,進入結果檢視 --> <result name="input" >/error.jsp</result> </action> </package></struts>
附加:contentType類型設定
上傳Action類UploadAction-簡單的儲存檔案方式
package com.struts;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport { private File uploadImage; //得到上傳的檔案 private String uploadImageContentType; //得到檔案的類型 private String uploadImageFileName; //得到檔案的名稱 public String execute(){ System.out.println("fileName:"+this.getUploadImageFileName()); System.out.println("contentType:"+this.getUploadImageContentType()); System.out.println("File:"+this.getUploadImage()); //擷取要儲存檔案夾的實體路徑(絕對路徑) String realPath=ServletActionContext.getServletContext().getRealPath("/upload"); File file = new File(realPath); //測試此抽象路徑名表示的檔案或目錄是否存在。若不存在,建立此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄。 if(!file.exists())file.mkdirs(); try { //儲存檔案 FileUtils.copyFile(uploadImage, new File(file,uploadImageFileName)); } catch (IOException e) { e.printStackTrace(); } return SUCCESS; } public File getUploadImage() { return uploadImage; } public void setUploadImage(File uploadImage) { this.uploadImage = uploadImage; } public String getUploadImageContentType() { return uploadImageContentType; } public void setUploadImageContentType(String uploadImageContentType) { this.uploadImageContentType = uploadImageContentType; } public String getUploadImageFileName() { return uploadImageFileName; } public void setUploadImageFileName(String uploadImageFileName) { this.uploadImageFileName = uploadImageFileName; } }
上傳成功頁面:
<body> 成功頁面. <br> </body>
上傳失敗頁面:
<!-- 引入struts2的標籤庫 --><%@ taglib uri="/struts-tags" prefix="s"%> <body> 跳轉錯誤頁面。。<br> <!-- fielderror標籤輸出action的fieldErrors屬性儲存的欄位錯誤,fieldErrors是一個map類型的屬性。 --> <s:fielderror /> <!-- 生產一個查看debug資訊的連結 --> <s:debug /> </body>
Struts2檔案上傳(基於表單的檔案上傳)