struts2中文件下载
2015-05-08 19:41
197 查看
下载的要求
下载(两头一流):Action中提供execute()方法中给流属性赋值
Content-Type:文件的MIME类型;
Content-Disposition:inline(默认值,表示在页面中打开),下载时设置为attachment;filename=xxx;
一个流:要下载文件的内容!
需要在struts.xml中配置
在Struts2下载需要由完成, 的类型必须为:stream;:但是我们需要给三个参数:两个头,一个流!
image/jpeg
attachment;fileName=a.jpg
流属性名称
Action代码
jsp页面
在中配置,它与表单参数一样,都可以被封装到Action 的属性中;
请求参数(动态的)由params拦截器来完成封装;
静态参数由staticParams拦截器来完成封装,它会在params拦截器之前执行;
所以params和staticParams如果处理的是同名属性,那么params优先级高
下载(两头一流):Action中提供execute()方法中给流属性赋值
Content-Type:文件的MIME类型;
Content-Disposition:inline(默认值,表示在页面中打开),下载时设置为attachment;filename=xxx;
一个流:要下载文件的内容!
需要在struts.xml中配置
在Struts2下载需要由完成, 的类型必须为:stream;:但是我们需要给三个参数:两个头,一个流!
image/jpeg
attachment;fileName=a.jpg
流属性名称
Action代码
package day03.simpleDownload; import java.io.FileInputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; import javax.servlet.ServletContext; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /** * 通过超链接来下载文件 * 1,获取超链接的参数 * 2,提供下载所需要的三个参数 * 3,在execute()方法中给三个参数赋值 * @author Spring * */ public class DownLoadAction extends ActionSupport { private String fname;//超链接参数 /* * 下载必须的三个参数 * inputName,contentType,contentDisposition * 而且这三个参数都需要在struts2中要用到,所以要有getXXX()方法; */ private String mimeType;//下载文件的MIME类型参数 private String contentName;//下载文件的提示名称 private InputStream is;//下载文件所需的对应的文件流 public String getMimeType() { return mimeType; } public String getContentName() { return contentName; } public InputStream getIs() { return is; } public String getFname() { return fname; } public void setFname(String fname) throws UnsupportedEncodingException { //因为用get提交方式会有中文乱码问题,详细可到-----查看 this.fname = new String(fname.getBytes("iso-8859-1"),"utf-8"); } public String execute() throws Exception { //System.out.println(fname); //在Action中获取ServletContext对象,通过ServletContext对象来获取文件的MIME类型和文件的绝对路径 ServletContext sac = ServletActionContext.getServletContext(); mimeType = sac.getMimeType(fname);//获取MIME类型 contentName = fname; String url = sac.getRealPath("/WEB-INF/downfile/"+fname);//获取文件的绝对路径 is = new FileInputStream(url);//获取下载文件的对应流 return "success"; } }struts.xml代码
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="false" /> <constant name="struts.multipart.saveDir" value="/tmp"/> <constant name="struts.multipart.maxSize" value="20971520"></constant> <package name="default" namespace="/" extends="struts-default"> <!-- 通过OGNL表达式来获取Action中的参数,实际是通过调用参数的getXXX()来获取的 --> <action name="downloadAction" class = "day03.simpleDownload.DownLoadAction"> <!-- result类型必须为stream类型,下载文件必须要有的三个参数 --> <result type="stream"> <param name="inputName">is</param> <param name="contentType">${mimeType}</param> <param name="contentDisposition">attachment;fileName=${mimeType}</param> </result> </action> </package> </struts>
jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <a href="downloadAction?fname=艾薇儿.jpg">下载艾薇儿.jpg</a> <br> <a href="downloadAction?fname=集合代码.pdf">下载集合代码.pdf</a> <br> </body> </html>静态参数(在struts.xml的action属性配置param);
在中配置,它与表单参数一样,都可以被封装到Action 的属性中;
请求参数(动态的)由params拦截器来完成封装;
静态参数由staticParams拦截器来完成封装,它会在params拦截器之前执行;
所以params和staticParams如果处理的是同名属性,那么params优先级高
相关文章推荐
- struts2 下载文件不能显示中文名字的问题
- struts2文件上传和下载
- struts2文件下载
- Struts2下载文件点取消出现的异常解决
- Struts2之文件上传与文件下载
- Struts2中POI在内存中生成文件并下载
- Struts2 文件上传下载 含下载时中文乱码
- struts2实现文件的上传与下载
- 深入分析JavaWeb Item47 -- Struts2拦截器与文件上传下载
- struts2文件下载,动态设置资源地址
- Struts2文件上传下载和表单重复提交问题
- Struts2实现文件的上传和下载
- 【Struts2】【文件的上传和下载】【fileUpload拦截器】
- Struts2文件的上传与下载
- Struts2下多文件的上传与下载(转)
- Struts2系列之文件上传下载
- Struts2文件下载
- struts2文件下载
- struts2 下载文件报 “java.lang.IllegalStateException”异常
- Struts2文件上传和下载