struts2注解实现文件下载

Struts2的配置就不讲了,重点讲下Action的写法。

DownloadAction.java:

package com.zrar.cms.action.front.suqiu.soft;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.opensymphony.xwork2.ActionSupport;
import com.zrar.cms.service.suqiu.ContentAttachService;
import com.zrar.cms.suqiu2_Entity.ContentAttach;
import com.zrar.cms.util.FilePathSptUtil;

@Results({@Result(name = "download", type = "stream", params = {
		"contentType", "application/octet-stream",
		"inputName", "inputStream", "contentDisposition",
		"attachment;filename=\"${downloadFileName}\"", "bufferSize",
		"4096" })})
public class DownloadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
	private String attachid;//附件id
	private String fileName;// 初始的通过param指定的文件名属性
	private ContentAttach contentAttachs;//附件对象
 
	public ContentAttach getContentAttachs() {
		return contentAttachs;
	}

	public void setContentAttachs(ContentAttach contentAttachs) {
		this.contentAttachs = contentAttachs;
	}

	@Autowired
	@Qualifier("contentAttachServiceImpl")
	private ContentAttachService contenAttachService;

	
	public String execute() throws Exception {
		if(StringUtils.isNotBlank(attachid)){
			contentAttachs = contenAttachService.getContentAttach(attachid);
			setFileName(contentAttachs.getAttachName());
		}
		return "download";
	}

	public InputStream getInputStream() throws Exception {
               // root项目上传图片路径,UPLOAD_ROOT_PATH定义为常量,从配置文件里取值
               // url就是附件在服务器上对应的路径
		String root = FilePathSptUtil.UPLOAD_ROOT_PATH;
		String url=root+contentAttachs.getAttachUrl();
		 
		return new FileInputStream(new File(url));
	}

	public String getDownloadFileName() {
		return fileName;
	}

	public void setFileName(String fileName) throws UnsupportedEncodingException {
		String agent = ServletActionContext. getRequest().getHeader("User-agent");
		//如果浏览器是IE浏览器,就得进行编码转换
		if(agent.contains("MSIE")){
			this.fileName = URLEncoder.encode(fileName, "UTF-8");
		}else{
			this.fileName = new String(fileName.getBytes(),"ISO-8859-1");  
		}
	}

	public String getAttachid() {
		return attachid;
	}
	public void setAttachid(String attachid) {
		this.attachid = attachid;
	}
}

其中主要使用的参数是:

contentType 指定下载文件的文件类型 —— application/octet-stream表示无限制

inputName 流对象名 ——比如这里写inputStream,它就会自动去找Action中的getInputStream方法。

contentDisposition 使用经过转码的文件名作为下载文件名 ——默认格式是attachment;filename="${fileName}",将调用该Action中的getFileName方法。

bufferSize 下载文件的缓冲大小

 

Jsp页面调用:

<a href="${ctx}/front/suqiu/ download.action?attachid=<s:property value="attachId"/>"><s:property  value="attachName"/></a>

前台页面点击效果如下:




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值