java 下载文件 兼容 ie 火狐 google 浏览器 ,解决下载文件乱码问题
来源:互联网 发布:同花顺mac 编辑:程序博客网 时间:2024/05/29 08:58
String filePath, String saveFileName) {
response.reset();
//response.setContentType("application/octet-stream;charset=utf-8");
response.setContentType(getContentType(saveFileName));
// response.setContentType("application/x-download");
OutputStream outp = null;
FileInputStream in = null;
try {
response.addHeader("Content-Disposition", "attachment;filename=\""+ encodeFilename(request,saveFileName)+"\"");//名称两边的双引号不能省略 兼容火狐 文件名中的空格
outp = response.getOutputStream();
in = new FileInputStream(filePath);
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) > 0) {
outp.write(b, 0, i);
}
outp.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
in = null;
}
if (outp != null) {
try {
outp.close();
} catch (IOException e) {
e.printStackTrace();
}
outp = null;
}
}
}
private static String encodeFilename(HttpServletRequest request,String fileName) throws UnsupportedEncodingException {
String agent =request.getHeader("USER-AGENT");
try {
// IE
if (null != agent && -1 != agent.indexOf("MSIE")) {
fileName = URLEncoder.encode(fileName, "UTF-8");
// Firefox
} else if (null != agent && -1 != agent.indexOf("Mozilla")) {
fileName = new String( fileName.getBytes("UTF-8"), "ISO-8859-1" );
}
} catch (UnsupportedEncodingException e) {
try {
fileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
return fileName;
}
private static String getContentType(String fileName) {
String ext = FileUtils.getExtension(fileName).toLowerCase();
if (ext.equals(".zip")) {
return "application/zip";
} else if (ext.equals(".xls") || ext.equals(".xlsx")) {
return "application/x-excel";
} else if (ext.equals(".doc") || ext.equals(".docx")) {
return "application/msword";
} else if (ext.equals(".pdf")) {
return "application/pdf";
} else if (ext.equals(".jpg") || ext.equals(".jpeg")) {
return "image/jpeg";
} else if (ext.equals(".gif")) {
return "image/gif";
} else if (ext.equals(".png")) {
return "image/png";
}else if(ext.equals(".bmp")){
return "image/bmp";
}
return "application/force-download";
}
- java 下载文件 兼容 ie 火狐 google 浏览器 ,解决下载文件乱码问题
- 解决.rar文件下载,火狐、谷歌可以下载,IE浏览器弹出乱码页面问题
- ie 火狐 下载文件乱码问题
- 解决火狐浏览器下载文件文件名乱码的问题
- java下载文件中文文件名乱码问题(ie,谷歌,火狐)
- IE和火狐浏览器下载文件,文件命乱码
- 解决FF浏览器和IE下载文件乱码问题
- 在ie浏览器下载文件乱码解决
- java实现浏览器下载文件,并解决兼容各浏览器的文件下载中文乱码
- ie 火狐 谷歌 浏览器下载,文件名字乱码,空格,不兼容问题解决
- 解决Java下载文件,文件名包含中文在IE浏览器乱码问题
- Firefox 火狐 浏览器下载文件文件名中文乱码解决
- 火狐浏览器下载文件名为乱码
- PHP解决浏览器 下载文件 中文名称乱码问题--兼容大多浏览器
- [置顶] PHP解决浏览器 下载文件 中文名称乱码问题--兼容大多浏览器
- 下载文件时兼容不同浏览器中文乱码解决
- php下载文件并且兼容IE浏览器
- Java: IE & Firefox下载文件中文乱码的兼容代码
- LeetCode Copy List with Random Pointer 分析解答
- 解析linux根文件系统的挂载过程
- fstat、stat和lstat 区别(转)
- Jacobi 方法
- Android开发中遇到的错误
- java 下载文件 兼容 ie 火狐 google 浏览器 ,解决下载文件乱码问题
- Method Swizzling
- Tomcat 配置(解决一闪而过的问题)
- hama程序出现Messages must never be behind the vertex in ID问题解决
- HDU 2044 一只小蜜蜂... 斐波那契问题 2041 超级楼梯
- C++不适合再去投资了
- 在Jmeter中使用自定义编写的Java测试代码
- android自定义弹出框popupwindow方式实现
- python2.7.5的安装使用