您的位置:首页 > 编程语言 > Java开发

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代码
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优先级高
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: