Java下载文件,中文文件名乱码问题解决 |
您所在的位置:网站首页 › 暑期班开课通知 › Java下载文件,中文文件名乱码问题解决 |
初始是这样的 String downloadfile = URLEncoder.encode(filepath,"UTF-8"); response.addHeader("Content-Disposition","attachment;filename=" + downloadfile ); 乱码就是乱在;filename=" + downloadfile 这里,对文件名的编码设定上。 使用URLEncoder.encode(filepath,"UTF-8")虽然可以解决在提示下载框中正确显示汉字文件名的问题,并且在选择保存,然后打开的情况下,文件名称也可以正确的显示。 但是在提示下载框中,选择直接打开,则文件名称变成了类似“%E7%BB%99%E7%94%A8%E6%88%B7%E6%8F%90%E4%BE%9B%E7%9A%84%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6%E5%90%8D”的样子。 为了解决这个问题,百度了好久,是这样的 String downloadfile = new String(filepath.getBytes("gb2312"),"iso8859-1"); response.addHeader("Content-Disposition","attachment;filename=" + downloadfile ); 还有这样 String userAgent = request.getHeader("User-Agent"); String formFileName = "员工表.xls"; // 针对IE或者以IE为内核的浏览器: if (userAgent.contains("MSIE") || userAgent.contains("Trident")) { formFileName = java.net.URLEncoder.encode(formFileName, "UTF-8"); } else { // 非IE浏览器的处理: formFileName = new String(formFileName.getBytes("UTF-8"), "ISO-8859-1"); } response.setHeader("Content-disposition",String.format("attachment; filename=\"%s\"", formFileName)); response.setContentType("multipart/form-data"); response.setCharacterEncoding("UTF-8"); 发现都不能解决问题,不懈努力,崩溃边缘,最终解决 String fileName = gTable.entityInfo()+".xls"; String fileNameURL = URLEncoder.encode(fileName, "UTF-8"); response.setHeader("Content-disposition", "attachment;filename="+fileNameURL+";"+"filename*=utf-8''"+fileNameURL); |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |