接著上一篇敘述:
二、文件上傳與下載
Struts2開發的三板斧,頁面jsp―配置文件struts2.xml―-還有動作類Action
文件上傳前提:
form表單的method必須是post
form表單的enctype必須是multipart/form-data
提供type=”file”的上傳輸入域
Struts 對文件上傳的支持的一些規則
1、單文件上傳
開發步驟:
1)、在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。這兩個文件可以從http://commons.apache.org/下載
2)、第二步:編寫upfile.jsp ,把form表的enctype設置為:“multipart/form-data“,如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><body> <s:actionerror/> <hr/> <s:fielderror></s:fielderror> <form action="${pageContext.request.contextPath}/upload1.action" method="post" enctype="multipart/form-data"><!-- 以MIME的方式傳遞--> 用戶名:<input type="text" name="username"/><br/> 靚照:<input type="file" name="photo"/><br/> <input type="submit" value="上傳"/> </form> </body>編寫錯誤頁面error.jsp
<body> 服務器忙,一會再試。 </body>
success.jsp
<body> 上傳成功</body>
3)、編寫UploadAction1 類:在Action類中添加屬性,屬性對應於表單中文件字段的名稱:
package com.itheima.actions;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;//文件上傳:fileUpload攔截器完成的public class UploadAction1 extends ActionSupport { private String username; private File photo;//和表單的上傳字段名保持一致。類型是File類型的private String photoFileName;//上傳的文件名private String photoContentType;//上傳文件的MIME類型//省略getter和setter方法public String upload(){ System.out.println(photoFileName+":"+photoContentType); //普通字段: System.out.println(username); //上傳字段:上傳到某個文件夾。存到應用的images目錄下String realPath = ServletActionContext.getServletContext().getRealPath("/images"); File directory = new File(realPath); if(!directory.exists()){ directory.mkdirs(); } try { FileUtils.copyFile(photo, new File(directory, photoFileName)); return SUCCESS; } catch (IOException e) { e.printStackTrace(); return ERROR; } }}在struts.xml文件中增加如下配置
<action name="upload1" method="upload"> <interceptor-ref name="defaultStack"> <param name="fileUpload.allowedTypes">image/jpeg,image/png</param> <param name="fileUpload.allowedExtensionsSet">jpg,jpeg,png</param> </interceptor-ref> <result>/success.jsp</result> <result name="error">/error.jsp</result> <result name="input">/index.jsp</result></action>
原理分析:
a 、FileUpload 攔截器負責處理文件的上傳操作, 它是默認的defaultStack 攔截器棧的一員. 攔截器有3 個屬性可以設置.
•maximumSize: 上傳文件的最大長度(以字節為單位), 默認值為2 MB
•allowedTypes: 允許上傳文件的類型, 各類型之間以逗號分隔
•allowedExtensions: 允許上傳文件擴展名, 各擴展名之間以逗號分隔可以在struts.xml 文件中覆蓋這3 個屬性
b、超出大小或非法文件的上傳,會報錯(轉向一個input的視圖)
通過:
<s:actionError/> <s:feildError/>顯示錯誤消息的提示
c、錯誤消息提示改為中文版:借助國際化的消息資源文件
如果是通過配置全局默認參數引起的錯誤,最好用全局的消息資源文件。
struts2默認的提示資源文件:struts2-core-**.jar 的org.apache.struts2的struts-message.properties文件中。比著key值覆蓋對應的value即可。
配置如下:
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是上傳文件的大小)
源碼:
修改顯示錯誤的資源文件的信息
第一步:創建新的資源文件例如fileuploadmessage.properties,放置在src下在該資源文件中增加如下信息
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}
第二步:在struts.xml文件加載該資源文件
<!-- 配置上傳文件的出錯信息的資源文件-->
<constant name="struts.custom.i18n.resources" value=“cn….xxx.fileuploadmessage“/>
2、多文件上傳
上傳多個文件, 可以使用數組或List,其他和單文件上傳類似。
package com.itheima.actions;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;//文件上傳:fileUpload攔截器完成的public class UploadAction2 extends ActionSupport { private String username; private File[] photo;//和表單的上傳字段名保持一致。類型是File類型的.數組或List private String[] photoFileName;//上傳的文件名private String[] photoContentType;//上傳文件的MIME類型public String upload(){ //上傳字段:上傳到某個文件夾。存到應用的images目錄下String realPath = ServletActionContext.getServletContext().getRealPath("/images"); File directory = new File(realPath); if(!directory.exists()){ directory.mkdirs(); } try { for(int i=0;i<photo.length;i++){ FileUtils.copyFile(photo[i], new File(directory, photoFileName[i])); } return SUCCESS; } catch (IOException e) { e.printStackTrace(); return ERROR; } }}3、文件下載
原理:struts2提供了stream結果類型,該結果類型就是專門用於支持文件下載功能的指定stream結果類型需要指定一個inputName參數,該參數指定一個輸入流,提供被下載文件的入口
編碼步驟:
1)、動作類DownloadAction :
package com.itheima.actions;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import java.net.URLEncoder;import org.apache.commons.io.FilenameUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class DownloadAction extends ActionSupport { private InputStream image;//用in有問題的private String filename;//文件名private long filesize; public InputStream getImage() { return image; } public void setImage(InputStream image) { this.image = image; } public String getFilename() { return filename; } public long getFilesize() { return filesize; } public String download() throws Exception{ //給image字節流賦值String fileRealPath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/classes/霉女.jpg"); filename = FilenameUtils.getName(fileRealPath); //方式一:中文文件要進行URL編碼// filename = URLEncoder.encode(filename, "UTF-8"); filesize = new File(fileRealPath).length(); System.out.println(filename); image = new FileInputStream(fileRealPath); return SUCCESS; }}struts.xml配置文件:主要是對stream類型的結果進行配置
<struts> <constant name="struts.devMode" value="true" /> <constant name="struts.ognl.allowStaticMethodAccess" value="true" /> <action name="download" method="download"> <result type="stream"> <param name="inputName">image</param><!--動作類中InputStream的字段名,需要在Action中提供getTargetFile方法,返回inputStream--> <param name="contentType">application/octet-stream</param><!--告訴瀏覽器響應頭,文件的MIME格式,調用Action中的getContentType方法--> <!-- 在struts.xml中使用OGNL表達式獲取動作類中屬性的值。 調用動作類中的getFilename()--> <!-- 中文文件名編碼:方式二.使用OGNL表達式,調用URLEncode的靜態方法--> <!-- 默認OGNL調用靜態方法是不行的,需要開啟一個常量開關.struts.ognl.allowStaticMethodAccess=true --> <param name="contentDisposition">attachment;filename=${@java.net.URLEncoder@encode(filename,'UTF-8')}</param><!-- 告訴瀏覽器的下載方式--> <param name="contentLength">${filesize}</param> </result> </action> </package></struts>攔截器和文件上傳就寫到這裡了,好累,不過成就感滿滿的。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。