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>

前台页面点击效果如下:

struts2注解实现文件下载_第1张图片


你可能感兴趣的:(exception,struts,String,action,download,Autowired)