基于struts2的文件上传下载
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獲取文件名is = ServletActionContext.getServletContext().getResourceAsStream("/images/"+fileName);//獲得文件流byte[] bytes = fileName.getBytes("utf-8");
fileName = new String(bytes, "iso-8859-1"); 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;?2.編碼問題
tomcat服務器端的編碼是utf-8,通常情況下不用fileName = new String(fileName.getBytes("iso-8859-1"),"UTF-8");設置編碼,而在輸出端使用
byte[] bytes = fileName.getBytes("utf-8"); fileName = new String(bytes, "iso-8859-1");設置編碼,編碼類型和瀏覽器端編碼類型有關
轉載于:https://www.cnblogs.com/Viveza/p/5895420.html
總結
以上是生活随笔為你收集整理的基于struts2的文件上传下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用shell批量转储表
- 下一篇: H5 的getImageData造成的大