在IE,FF,chrome中文件名都不会乱码,在其他浏览器中,请参考《不同浏览器上中文文件名的下载乱码问题》
jsp:
<a href="<%=path %>/downloadProFile.action?fileName=${data.app_name }&fileUrl=${data.app_url }">下载</a>
strtus:
<action name="downloadProFile" class="com.actions.DownloadAction">
<result name="success" type="stream">
<!--设置文件类型-->
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<!--下载文件流-->
<param name="inputName">inputStream</param>
<!--添加参数,即就是下载的名称-->
<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
<!--缓存-->
<param name="bufferSize">4096</param>
</result>
</action>
DownloadAction.java
package com.actions;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.actions.common.BaseAction;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
/**
* 功能:下载
* @author Adam
*
*/
public class DownloadAction extends BaseAction {
//兼容linux
String fs = System.getProperties().getProperty("file.separator");
protected String configPath = "attached";
protected String dirTemp = "attached"+fs+"temp";
public String downloadFileName;
public InputStream inputStream;
/**
* 功能:设置返回下载文件名
* @return
*/
public String getDownloadFileName() {
String fileName = getRequest().getParameter("fileName").trim();
String agent = getRequest().getHeader("USER-AGENT");
try {
fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");
if(null != agent && -1 != agent.indexOf("MSIE")){
fileName = URLEncoder.encode(fileName,"UTF8");
//将加号还原为空格(URLEncoder.encode()会将空格转成加号)
fileName = fileName.replaceAll("\\+", "%20");
}else if(null != agent && -1 != agent.indexOf("Mozilla")){
fileName = MimeUtility.encodeText(fileName,"UTF8","B");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return fileName;
}
public void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
/**
* 功能:下载文件,设置文件流
* File.separator
* @return
*/
public InputStream getInputStream() {
String fileUrl = getRequest().getParameter("fileUrl").trim();
fileUrl = fileUrl.replace("/", fs);
String savePath = getSession().getServletContext().getRealPath(fs) + configPath;
String realPath = fs + configPath + fs + fileUrl;
File file = new File(savePath + fs + fileUrl);
if (file.isFile() && file.exists()) {
getLog().logger.error("realPath:" + realPath);
System.out.println(realPath);
return ServletActionContext.getServletContext().getResourceAsStream(realPath);
}else{
String tempPath = getSession().getServletContext().getRealPath(fs) + dirTemp;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
String ymd = sdf.format(new Date());
tempPath += fs + ymd + fs;
File dirTempFile = new File(tempPath);
if (!dirTempFile.exists()) {
dirTempFile.mkdirs();
}
File f = new File(tempPath + this.getDownloadFileName());
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
String inExistPath = fs + dirTemp + fs + ymd + fs + this.getDownloadFileName();
return ServletActionContext.getServletContext().getResourceAsStream(inExistPath);
}
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String execute() {
return "success";
}
}