Struts2,sping文件下载

来源:互联网 发布:怎么样改淘宝密码忘了 编辑:程序博客网 时间:2024/05/04 23:39

1.struts.xml中加入如下代码

<action name="download" class="download"  method="download">     <result name="success" type="stream">    <param name="contentType">application/octet-stream;charset=ISO8859-1</param>    <param name="inputName">inputStream</param>    <!--     使用经过转码的文件名作为下载文件名,downloadFileName属性 对应action类中的方法     getDownloadFileName()    -->    <param name="contentDisposition">attachment;filename="${downloadFileName}"</param>    <param name="bufferSize">4096</param>   </result></action>

Application.xml:

<bean id="download" class="com.oa.util.DownloadBean"><property name="dao"><ref bean="hibernateTemplateDaoImp" /></property></bean>

2.action

public class DownloadBean {private HibernateTemplateDaoImp dao;private String fileName;private InputStream inputStream;private String id;public InputStream getInputStream() throws Exception {Upfile data = (Upfile) dao.get(Upfile.class, id);fileName = data.getFilename();String realPath = ServletActionContext.getServletContext().getRealPath("/")+ "uploadFile/" + fileName;File file = new File(realPath);inputStream = new FileInputStream(file);return inputStream;}public String getFileName() {return fileName;}public void setInputStream(InputStream inputStream) {this.inputStream = inputStream;}public String download() {return "success";}public void setFileName(String fileName) {this.fileName = fileName;}/** 提供转换编码后的供下载用的文件名 */public String getDownloadFileName() {String downFileName = fileName;try {downFileName = new String(downFileName.getBytes(), "ISO8859-1");} catch (UnsupportedEncodingException e) {e.printStackTrace();}return downFileName;}public String getId() {return id;}public void setId(String id) {this.id = id;}public HibernateTemplateDaoImp getDao() {return dao;}public void setDao(HibernateTemplateDaoImp dao) {this.dao = dao;}}


0 0
原创粉丝点击