码迷,mamicode.com
首页 > 其他好文 > 详细

struts文件下载

时间:2018-11-28 12:17:00      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:down   download   buffers   app   ack   col   puts   vat   ESS   

  //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文件下载

标签:down   download   buffers   app   ack   col   puts   vat   ESS   

原文地址:https://www.cnblogs.com/zzy666/p/10030225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!