基于struts2的文件上传下载
1.struts.xml
1 <struts> 2 <constant name="struts.multipart.maxSize" value="209715200" /><!-- 設(shè)置文件上傳大小,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><!-- 文件上傳成功后跳轉(zhuǎn)頁(yè)面 --> 7 <result name="message">/message.jsp</result><!-- 上傳失敗跳轉(zhuǎn)頁(yè)面 --> 8 <interceptor-ref name="defaultStack"> 9 <param name="fileUpload.allowedExtensions">jpg,png</param><!-- 設(shè)置上傳格式 --> 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><!-- 指定返會(huì)數(shù)據(jù)的類(lèi)型 --> 17 <param name="contentDisposition">attachement;filename=${fileName}</param><!-- 指定用戶(hù)下載的文件的類(lèi)型和名稱(chēng) --> 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; //輸入流,先將文件讀入服務(wù)端的內(nèi)存private String fileName; //隨意 public String execute() throws FileNotFoundException, UnsupportedEncodingException{fileName = "1.png";//設(shè)置路徑,在項(xiàng)目中通過(guò)連接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.?下載時(shí)出現(xiàn)500:Can not find a java.io.InputStream with the name [is] in the invocation stack. Check the <param nam
解決思路:1)查看is輸入流是否為null,若為空則沒(méi)有獲取到正確路徑,檢查文件名是否正確
? 2)在action中沒(méi)有寫(xiě)配置文件中"<param name="inputName">"后面屬性的那個(gè)get方法
? 3)當(dāng)采用 return ServletActionContext.getServletContext().getResourceAsStream("...") 這種方法獲得輸入流的時(shí)候,要保證文件位置在 ServletContext 當(dāng)中,就是說(shuō)要在當(dāng)前的應(yīng)用上下文中,如果想要獲得外部文件 譬如 D盤(pán)中的某個(gè)文件,那么就要自己創(chuàng)建輸入流才可以,如:
File file = new File("D://spring.doc"); InputStream is = new FileInputStream(file); return is;?2.編碼問(wèn)題
tomcat服務(wù)器端的編碼是utf-8,通常情況下不用fileName = new String(fileName.getBytes("iso-8859-1"),"UTF-8");設(shè)置編碼,而在輸出端使用
byte[] bytes = fileName.getBytes("utf-8"); fileName = new String(bytes, "iso-8859-1");設(shè)置編碼,編碼類(lèi)型和瀏覽器端編碼類(lèi)型有關(guān)
轉(zhuǎn)載于:https://www.cnblogs.com/Viveza/p/5895420.html
總結(jié)
以上是生活随笔為你收集整理的基于struts2的文件上传下载的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 用shell批量转储表
- 下一篇: H5 的getImageData造成的大