application/octet-stream
inputStream
attachment;filename="${downfilename}"
4096
java代码(可见附件):
package com.telewave.policesystemweb.szrk;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.xml.messaging.saaj.packaging.mime.internet.MimeUtility;
/**
* 文件下载
* @author Luxh
*/
public class DownloadAction extends ActionSupport {
private static final long serialVersionUID = -3036349171314867490L;
//文件名
private String fileName;
private String downfilename;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) throws UnsupportedEncodingException {
//用UTF-8重新编码文件名,解决中文乱码
this.fileName = fileName;
}
public String getDownfilename() {
return downfilename;
}
public void setDownfilename(String downfilename) {
this.downfilename = downfilename;
}
public InputStream getInputStream() throws UnsupportedEncodingException, FileNotFoundException{
int i = fileName.lastIndexOf("\\");
String str = fileName.substring(i+1);//截取文件名
HttpServletRequest request = ServletActionContext.getRequest();
String userAgent = request.getHeader("User-Agent");
userAgent = userAgent.toLowerCase();
String filernd = "";
//针对不同浏览器,进行不同的编码,目前支持火狐、谷歌、ie
if (null != userAgent){
if (-1 != userAgent.indexOf("firefox")) {//Firefox
filernd = "=?UTF-8?B?" + (new String(org.apache.commons.codec.binary.Base64.encodeBase64(str.getBytes("UTF-8"))))+ "?=";
}else if (-1 != userAgent.indexOf("chrome")) {//Chrome
filernd = new String(str.getBytes(), "ISO8859-1");
} else {//IE7+
filernd = java.net.URLEncoder.encode(str, "UTF-8");
filernd = StringUtils.replace(filernd, "+", "%20");//替换空格
}
} else {
filernd = str;
}
setDownfilename(filernd);
// IE浏览器可以用以下方式,不通过xml文件attachment;filename="${downfilename}",但是非ie不支持以下这种方式,
// 只能通过xml文件方式读取action中的downfilename
// ServletActionContext.getResponse().setHeader(
// "Content-Disposition",
// "attachment;fileName="+filernd);
// + java.net.URLEncoder.encode(str, "ISO-8859-1"));
InputStream inputStream = new FileInputStream(fileName);
return inputStream;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}