Struts2文件下载

1. download.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/DownloadAction_download?name=image.jpg">image.jpg</a>
<a href="${pageContext.request.contextPath }/DownloadAction_download?name=ListViewDemo.zip">ListViewDemo.zip</a>
<a href="${pageContext.request.contextPath }/DownloadAction_download?name=压缩包.zip">压缩包.zip</a>
</body>

</html>


2. DownloadAction

package com.wenhao.down;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


import javax.servlet.ServletContext;


import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionSupport;


import sun.misc.BASE64Encoder;


public class DownloadAction extends ActionSupport{

private String name;
public String getName() {
return name;
}
public void setName(String name) throws UnsupportedEncodingException {
this.name = new String(name.getBytes("UTF-8"),"UTF-8");
}



// 设置两个头和一个流:
// 两头和一个流需要在结果页面的类型为stream中进行设置.
// 提供几个方法:
// getContentType:提供了一个获得文件的MIME类型的方法.---getContentType:相当于类中有一个属性contentType.
public String getContentType(){
// 获得ServletCOntext对象
ServletContext application = ServletActionContext.getServletContext();
// 获得文件的MIME类型:
String type = application.getMimeType(name);
// 返回MIME类型
return type;
}

// 提供一个活的文件名的方法:相当于Action中有一个属性:fileName
public String getFileName() throws IOException{
// 根据浏览器的类型进行编码:
// IE用URL编码,Firefox用Base64
// 获得浏览器类型:
String agent = ServletActionContext.getRequest().getHeader("User-Agent");
// 获得了编码后的文件名
String filename = encodeDownloadFilename(name,agent);
return filename;
}

/**
* 下载文件时,针对不同浏览器,进行附件名的编码
* @param filename 下载文件名
* @param agent 客户端浏览器
* @return 编码后的下载附件名
* @throws IOException
*/
public String encodeDownloadFilename(String filename, String agent) throws IOException{

if (agent.contains("MSIE")) {
// IE浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
// 火狐浏览器
BASE64Encoder base64Encoder = new BASE64Encoder();
filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
} else {
// 其它浏览器
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;


}


// 提供一个获得文件的输入流
// 相当于类中有一个属性inputStream
public InputStream getInputStream() throws FileNotFoundException{
// 获得本地路径:
ServletContext application = ServletActionContext.getServletContext();
// 获得download路径的磁盘绝对路径
String path = application.getRealPath("/download");
InputStream is = new FileInputStream(path+"/"+name);
return is;
}

public String download() {
return "success";
}

}


3.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>
<package name="download" extends="struts-default" >
<action name="DownloadAction_*" class="com.wenhao.down.DownloadAction" method="{1}">
<result name="success" type="stream">
    <param name="contentType">${contentType}</param>
    <param name="contentDisposition">attachment;filename=${fileName}</param>
    <param name="inputStream">${inputStream}</param>
</result>
</action>
</package>
</struts>

猜你喜欢

转载自blog.csdn.net/A_jungle/article/details/80070396