1. 程式人生 > >struts文件下載

struts文件下載

nco cati posit isp public 獲取 align cat ica

  //DownloadAction類
1 package action;
3 import java.io.BufferedInputStream; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.InputStream; 8 9 import org.apache.struts2.ServletActionContext; 10 11 import com.opensymphony.xwork2.Action;
12 13 public class DownloadAction { 14 // 請求參數 15 private String fileName; 16 // action下載的目錄 17 private String inputPath; 18 19 // 定義get方法,用於獲取輸入流 20 public InputStream getInputStream() throws FileNotFoundException { 21 22 String absInputPath = ServletActionContext.getServletContext()
23 .getRealPath(inputPath); 24 FileInputStream fis = new FileInputStream(absInputPath + File.separator 25 + fileName); 26 BufferedInputStream bis = new BufferedInputStream(fis); 27 return bis; 28 29 } 30 31 public String execute() { 32
33 return Action.SUCCESS; 34 35 } 36 37 public String getFileName() { 38 return fileName; 39 } 40 41 public void setFileName(String fileName) { 42 this.fileName = fileName; 43 } 44 45 public String getInputPath() { 46 return inputPath; 47 } 48 49 public void setInputPath(String inputPath) { 50 this.inputPath = inputPath; 51 } 52 53 }
  
//struts.xml
<action name="download" class="action.DownloadAction">
<param name="inputPath">/upload</param>
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
attachment;fileName="${fileName}"
</param>
<param name="bufferSize">4096</param>
</result>
</action>

  




struts文件下載