標籤:
1.struts.xml
1 <struts> 2 <constant name="struts.multipart.maxSize" value="209715200" /><!-- 設定檔案上傳大小,2097152=2M --> 3 <package name="action" extends="struts-default" namespace="/"> 4 <!-- 檔案上傳 --> 5 <action name="upload" class="com.fileupload.action.UploadAction" method="execute"> 6 <result name="success">load_success.jsp</result><!-- 檔案上傳成功後跳轉頁面 --> 7 <result name="message">/message.jsp</result><!-- 上傳失敗跳轉頁面 --> 8 <interceptor-ref name="defaultStack"> 9 <param name="fileUpload.allowedExtensions">jpg,png</param><!-- 設定上傳格式 -->10 </interceptor-ref>11 </action>12 <!-- 檔案下載 -->13 <action name="download" class="com.fileupload.action.DownloadAction" method="execute">14 <result type="stream">15 <param name="inputName">is</param> <!-- 指定返迴流的名字 -->16 <param name="contentType">application/pdf/jpg</param><!-- 指定返會資料的類型 -->17 <param name="contentDisposition">attachement;filename=${fileName}</param><!-- 指定使用者下載的檔案的類型和名稱 -->18 </result>19 </action>20 </package>21 </struts>
2.上傳action
1 import java.io.File; 2 import java.io.IOException; 3 import org.apache.commons.io.FileUtils; 4 import org.apache.struts2.ServletActionContext; 5 6 public class UploadAction { 7 private File img; //檔案 8 private String imgFileName; //檔案名稱:檔案+FileName 9 10 public String execute() throws IOException{11 12 if(img != null){13 String path = ServletActionContext.getServletContext().getRealPath("/images");14 File destFile = new File(path, imgFileName);15 FileUtils.copyFile(img, destFile);16 return "success";17 }18 return "message";19 }20 21 public File getImg() {22 return img;23 }24 public void setImg(File img) {25 this.img = img;26 }27 public String getImgFileName() {28 return imgFileName;29 }30 public void setImgFileName(String imgFileName) {31 this.imgFileName = imgFileName;32 }33 }
3.下載action
import java.io.FileNotFoundException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import org.apache.struts2.ServletActionContext;public class DownloadAction { private InputStream is; //輸入資料流,先將檔案讀入服務端的記憶體 private String fileName; //隨意 public String execute() throws FileNotFoundException, UnsupportedEncodingException{ fileName = "1.png";//設定路徑,在項目中通過串連DB擷取檔案名稱 fileName = new String(fileName.getBytes("iso-8859-1"),"UTF-8");//設定編碼 is = ServletActionContext.getServletContext().getResourceAsStream("/images/"+fileName);//獲得檔案流 return "success"; } public InputStream getIs() { return is; } public void setIs(InputStream is) { this.is = is; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; }}
[注意]:
1. 下載時出現500:Can not find a java.io.InputStream with the name [is] in the invocation stack. Check the <param nam
解決思路:1)查看is輸入資料流是否為null,若為空白則沒有擷取到正確路徑,檢查檔案名稱是否正確
2)在action中沒有寫設定檔中"<param name="inputName">"後面屬性的那個get方法
3)當採用 return ServletActionContext.getServletContext().getResourceAsStream("...") 這種方法獲得輸入資料流的時候,要保證檔案位置在 ServletContext 當中,就是說要在當前的應用上下文中,如果想要獲得外部檔案 譬如 D盤中的某個檔案,那麼就要自己建立輸入資料流才可以,如:
File file = new File("D://spring.doc"); InputStream is = new FileInputStream(file); return is;
基於struts2的檔案上傳下載