兼容各浏览器的文件下载时中文名称乱码的解决方案

来源:互联网 发布:uploadify java 编辑:程序博客网 时间:2024/05/09 21:40
public class DownloadServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // codes..
        String name = "中文名 带空格 的测试文件.txt";
        String userAgent = request.getHeader("User-Agent");
        byte[] bytes = userAgent.contains("MSIE") ? name.getBytes() : name.getBytes("UTF-8"); // name.getBytes("UTF-8")处理safari的乱码问题
        name = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
        response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", name)); // 文件名外的双引号处理firefox的空格截断问题
        // codes..
    }

}


这段代码处理了文件下载时不同浏览器解析中文文件名所出现的乱码问题和firefox的空格截断问题,在IE9, chrome, opera, safari, firefox下均测试通过。 

0 0
原创粉丝点击