標籤:stack support mic actions 攔截器 ace result pac 單位
ud_upload.jsp
<s:form action="fileupload" enctype="multipart/form-data"> <s:textfield label="照片描述" name="desc"></s:textfield> <s:file label="檔案1" name="file1"></s:file> <s:submit value="上傳"></s:submit> </s:form>
ud_download.jsp
<s:url var="temp1" action="filedownload" ></s:url> <s:a href="%{temp1}" >下載bload.png</s:a>
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <constant name="struts.configuration.xml.reload" value="true" /> <constant name="struts.custom.i18n.resources" value="messageResource" /> <package name="p1" namespace="/" extends="struts-default"> <action name="fileupload" class="org.ah.s2.C1" method="fileupload"> <!-- 上傳需要(內建)攔截器,傳入參數 --> <interceptor-ref name="fileUpload"> <!-- 定義允許上傳的類型 --> <param name="allowedTypes">image/jpeg,image/png</param> <!-- 檔案大小,單位:byte,不能用乘法計算,只能寫最終數字 --> <!-- 35k --> <param name="maximumSize">35850</param> </interceptor-ref> <interceptor-ref name="defaultStack" /> <result name="success" type="dispatcher"> /ud_download.jsp </result> <!-- 檔案被過濾掉,將返回input --> <result name="input">/ud_upload.jsp</result> </action>
<action name="filedownload" class="org.ah.s2.C1" method="filedownload"> <!-- 只有一個result子項目,不用name --> <result type="stream"> <param name="contentType">image/png</param> <!-- fileName對應下載後的檔案名稱,這裡就用Action中的變數了 --> <param name="contentDisposition">attachment;fileName=${downLoadAh}</param> <param name="inputName">inputStream</param> </result> </action> </package></struts>
image/jpeg,不是jpg!
如果上傳的檔案不符合指定的要求,會回顯錯誤資訊。這些錯誤資訊基於i18n,存放在struts-messages.properties設定檔中,所以需要配置struts.custom.i18n.resources
Action:
package org.ah.s2;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionSupport;public class C1 extends ActionSupport { private String desc;// 描述 private File file1;// 上傳檔案。java.io // 以下變數直接可得,檔案名稱file1+固定尾碼,都需要getter setter方法 private String file1FileName;// 上傳檔案名稱 private String file1ContentType;// 上傳檔案類型 public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public File getFile1() { return file1; } public void setFile1(File file1) { this.file1 = file1; } public String getFile1FileName() { return file1FileName; } public void setFile1FileName(String file1FileName) { this.file1FileName = file1FileName; } public String getFile1ContentType() { return file1ContentType; } public void setFile1ContentType(String file1ContentType) { this.file1ContentType = file1ContentType; } /** * 檔案上傳 * * @return * @throws IOException */ public String fileupload() throws IOException { // \t:定位字元 System.out.println("File name:" + this.file1FileName + "\t" + "ContentType:" + this.file1ContentType + "\t" + "描述:" + this.desc); // 檔案拷貝 FileInputStream fis = new FileInputStream(this.file1); FileOutputStream fos = new FileOutputStream("D:\\fileupload\\" + this.file1FileName); byte[] bs = new byte[1024]; int real = fis.read(bs); while (real > 0) { fos.write(bs, 0, real); real = fis.read(bs); } fos.close(); fis.close(); return Action.SUCCESS; } // -------------------------------------------------- // 下載時預設名稱,只需getter方法即可 private String downLoadAh; public String getDownLoadAh() { return downLoadAh; } /** * 檔案下載 * @return */ public String filedownload() { downLoadAh = "border_1.png"; return Action.SUCCESS; } // 用於下載的檔案輸入資料流
// 對應:<param name="inputName">inputStream</param>
public InputStream getInputStream() { FileInputStream fis = null; try { fis = new FileInputStream(new File("D:\\fileupload\\" + downLoadAh)); } catch (FileNotFoundException e) { e.printStackTrace(); } return fis; }}
messageResource.properties
struts.messages.error.file.too.large=\u6587\u4EF6\u592A\u5927struts.messages.error.file.extension.not.allowed=\u6587\u4EF6\u7C7B\u578B\u4E0D\u5339\u914Dstruts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u7C7B\u578B\u4E0D\u6B63\u786E
Struts2:上傳下載