Struts2,sping文件下载

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;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值