Struts2 file download

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");}











// Set two headers and one stream:
// Two headers and one stream need to be set in the type of the result page as stream.
// Provides several methods:
// getContentType: Provides a method to get the MIME type of the file.- --getContentType: Equivalent to an attribute contentType in the class.
public String getContentType(){
// Get the ServletCOntext object
ServletContext application = ServletActionContext.getServletContext();
// Get the MIME type of the file:
String type = application.getMimeType(name) ;
// Return MIME type
return type;
} // A method to provide a live file name: equivalent to a property in Action: fileName public String getFileName() throws IOException{ // Encode according to the browser type: // IE uses URL encoding, Firefox uses Base64 // Get the browser type: String agent = ServletActionContext.getRequest().getHeader("User-Agent"); // Get the encoded file name








String filename = encodeDownloadFilename(name,agent);
return filename;
} /** * When downloading a file, encode the attachment name for different browsers * @param filename Download file name * @param agent Client browser * @return Encoded download attachment name * @throws IOException */ public String encodeDownloadFilename(String filename, String agent) throws IOException{ if (agent.contains("MSIE")) { // IE browser filename = URLEncoder.encode(filename, "utf-8"); filename = filename.replace("+", " "); } else if (agent.contains("Firefox")) { // Firefox BASE64Encoder base64Encoder = new BASE64Encoder(); filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";} else {



















// Other browsers
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;


} // Provide an input stream to get the file // Equivalent to a property inputStream in the class public InputStream getInputStream() throws FileNotFoundException{ // Get the local path: ServletContext application = ServletActionContext.getServletContext(); // Get the absolute disk path of the download path 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>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324810802&siteId=291194637