解决IE及其他浏览器下载文件标题中文乱码问题
近期些项目遇到下载文件中文名称乱码问题,在网上看了一些博客,总结出以下内容,废话不说,直接上代码。
//首先给一个生成文件的路径
String filePath = "C:\Users\admin\Desktop\pdf";
//判断文件夹存不存在,不存在则创建
File wjjfile = new File(filePath);
if (!wjjfile.exists()) {
wjjfile.mkdirs();
}
//生成一个随机数作为生成文件名
String code = String.valueOf(new Random().nextInt(899999) + 100000);
String fileName = code+".pdf";
String clientFileName = "202x年度xxx花名册.pdf";
//创建pdf 使用itext技术手动画PDF,这一步网上有很多例子
if(BysjymcPDFUtil.createPDF(filePath+fileName, data)){
//打印日志
}
//判断文件存不存在
File file=new File(filePath,fileName);
if(!file.isFile()){
throw new FileNotFoundException("");
}
BufferedInputStream bis= new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = null;
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
if (request.getScheme() == "https" || request.getScheme() == "http") {
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
}
long fileLength = file.length();
response.setContentType("application/x-msdownload;");
String browser = "";
try {
browser = request.getHeader("User-Agent");
if (browser.indexOf("MSIE 6.0") > -1 || browser.indexOf("MSIE 7.0") > -1 ) {
// IE6, IE7 浏览器
response.addHeader("content-disposition", "attachment;filename="+ new String(clientFileName.getBytes(), "ISO8859-1"));
} else if (browser.indexOf("MSIE 8.0") > -1) {
// IE8
response.addHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(clientFileName, "UTF-8"));
} else if (browser.indexOf("MSIE 9.0") > -1) {
// IE9
response.addHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(clientFileName, "UTF-8"));
}
else if (browser.indexOf("MSIE 10") > 0) {
// IE10
response.addHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(clientFileName, "UTF-8"));
} //null != agent && -1 != agent.indexOf("like Gecko")
else if (browser.indexOf("Gecko") > 0 && browser.indexOf("rv:11") > 0) {
// IE11
response.addHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(clientFileName, "UTF-8"));
}
else if (browser.indexOf("Chrome") > -1) {
// 谷歌
response.addHeader("content-disposition","attachment;filename*=UTF-8''" + URLEncoder.encode(clientFileName, "UTF-8"));
} else if (browser.indexOf("Safari") > -1) {
// 苹果
response.addHeader("content-disposition", "attachment;filename="+ new String(clientFileName.getBytes(), "ISO8859-1"));
} else {
// 火狐或者其他的浏览器
response.addHeader("content-disposition","attachment;filename*=UTF-8''" + URLEncoder.encode(clientFileName, "UTF-8"));
}
} catch (Exception e) {
e.printStackTrace();
}
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
声明:希望对类似问题的解决有效,转载请附上原文出处链接和本声明。