javaweb文件下载中文名乱码问题
来源:互联网 发布:手机qq防盗软件 编辑:程序博客网 时间:2024/04/30 23:26
IE:通过URLEncoder对filename进行UTF8编码
其他浏览器(firefox、chrome、safari、opera),通过字节转换成ISO-8859-1
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) { filename = URLEncoder.encode(filename, "UTF-8");} else { filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");}
public class DataExportAction extends Action{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){ DataForm formBean = (DataForm)form; DataService dataService = (DataService)getBean("DataService"); ...// 此处省略对 formBean 进行处理 response.reset(); fileName = "中文乱码问题"; ServletOutputStream out = response.getOutputStream(); String agent = request.getHeader("User-Agent"); if(agent != null && agent.toUpperCase().indexOf("MSIE") != -1){ response.setHeader("Content-Disposition","attachment; fileName=" +URLEncoder.encode(fileName,"utf-8") + ".xls") }else{ response.setHeader("Content-Disposition","attachment; fileName=" + new String(fileName.getBytes("utf-8"),"iso-8859-1") + ".xls");}//定义输出类型response.setContentType("APPLICATION/msexcel");//...DataService.exportDataToExcel(formBean,out);out.close();response.flushBuffer();}
通过分析userAgent属性来判断浏览器的类型及版本:
Windows操作系统浏览器系列:
IE浏览器系列:
特征表现:均以 “mozilla/” 开头,”msie x.0;” 中的x表示其版本;
判断方法:粗略判断可以只检索 “msie x.0;” 字符串即可,严格判断可检索 “mozilla/x.0 (compatibal; msie x.0; windows nt”,不过一般没有这个必要
Windows版Firefox:
特征表现:以”mozilla/x.0”开头,包含”windows nt”,”gecko/”和”firefox/” ;
判断方法:粗略判断可以只检索 “firefox/”和”windows nt” 字符串,严格判断可以检索”mozilla/” ,”windows nt”,”gecko/”和”firefox/” 四个字符串;
Windows版Chrome:
特征表现: 以”mozilla/x.0”开头,包含”windows nt”,”chrome/”,同时包含”applewebkit/”,”safari/”;
判断方法:粗略判断可以只检索 “windows nt”和”chrome/”字符串,严格判断可以同时检索 “mozilla/” ,”windows nt”,”applewebkit/”,”safari/”,”chrome/” 五个字符串;
Windows版Opera:
特征表现:以”opera/”开头,含有”windows nt”,”presto/” 字符串;
判断方法:粗略判断只检索 “windows nt”和”opera/”字符串,严格判断同时检索 “opera/”,”windows nt” 和 “presto/”;
Windows版Safari:
特征表现:以”mozilla/”开头,同时含有”windows nt”,”applewebkit/”,”safari/”;
判断方法:粗略判断可以检索含有 “windows nt”,”safari/” 同时不包含 “chrome/”,严格判断需要同时含有”mozilla/”,”windows nt”,”applewebkit/”,”safari/”但是不包含”chrome/”;
小结:Windows操作系统上的浏览器userAgent均包含”windows nt”字符串来表征windows操作系统。
- javaweb文件下载中文名乱码问题
- 下载文件:中文名乱码问题
- Firefox下载文件时中文名乱码问题
- Firefox下载文件时中文名乱码问题
- 彻底解决中文名文件下载和下载文件内容乱码问题!!!!!
- 彻底解决中文名文件下载和下载文件内容乱码问题!!!!!
- 下载中文名文件问题
- 文件下载时中文名乱码
- firefox下载中文名文件乱码
- Struts2文件下载中文名乱码
- 在IE下-下载文件-文件名中文名乱码问题
- JavaWeb项目下中文名文件的下载
- asp.net 下载文件 中文名乱码
- Android 文件下载中文名乱码的解决办法
- 下载文件中文名出现乱码 java
- 下载时的中文名文件乱码
- java下载文件的时候中文名乱码
- Java文件下载中文名不显示&乱码
- struct和typedef struct
- servlet编码,解决中文乱码
- React 双向绑定的实现
- Move Zeroes -- leetcode
- struct meminfo分析
- javaweb文件下载中文名乱码问题
- Makefile编写
- JS获取标签属性值
- 个股牛熊市场的判断标准(学习笔记)
- HUD显示文字,osgText的使用
- Source Insight 3 出现错误:Error:File read error
- MD5和SHA1
- was常见问题
- swift学习- 扩展(二十一)