博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts2注解实现文件下载
阅读量:4069 次
发布时间:2019-05-25

本文共 2771 字,大约阅读时间需要 9 分钟。

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页面调用:

">

前台页面点击效果如下:

转载地址:http://qphji.baihongyu.com/

你可能感兴趣的文章
Python学习笔记之特点
查看>>
Python学习笔记之安装
查看>>
shell 快捷键
查看>>
VIM滚屏操作
查看>>
EMC 2014存储布局及十大新技术要点
查看>>
linux内核内存管理(zone_dma zone_normal zone_highmem)
查看>>
将file文件内容转成字符串
查看>>
循环队列---数据结构和算法
查看>>
优先级队列-数据结构和算法
查看>>
链接点--数据结构和算法
查看>>
servlet中请求转发(forword)与重定向(sendredirect)的区别
查看>>
Spring4的IoC和DI的区别
查看>>
springcloud 的eureka服务注册demo
查看>>
eureka-client.properties文件配置
查看>>
MODULE_DEVICE_TABLE的理解
查看>>
platform_device与platform_driver
查看>>
platform_driver平台驱动注册和注销过程(下)
查看>>
.net强制退出主窗口的方法——Application.Exit()方法和Environment.Exit(0)方法
查看>>
c# 如何调用win8自带的屏幕键盘(非osk.exe)
查看>>
build/envsetup.sh 简介
查看>>