- <%@ page contentType="application/msword;charset=utf-8"%>
- <%
- String fileName = new String("这是一个例子.doc".getBytes("GBK"),"ISO8859_1");
- response.addHeader("Content-Disposition", "attachment;filename="+ fileName);
- %>
- 或
- response.setContentType("application/x-msdownload;");
- response.setCharacterEncoding("UTF-8");
- fileName = URLEncoder.encode(fileName, "UTF-8");
- response.addHeader("Content-Disposition", "attachment; filename=\""+ fileName+"\"");
- 日文好像使用UTF-8不行,改用Shift-JIS就可以了!
-
第一种: 设置 response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));这里将文件名编码成UTF-8的格式,就不会出现URL出错了。IE6下注意中文文字不能超过超过17个。
第二种:设置response.setHeader( "Content-Disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) );将中文名编码为ISO8859-1的方式。不过该编码只支持简体中文.
按照上诉方式,可以综合一下两种方式解决绝大部分中文问题。
fileName = URLEncoder.encode(fileNameSrc,"UTF-8");
if(fileName.length()>150)//解决IE 6.0 bug
fileName=new String(fileNameSrc.getBytes("GBK"),"ISO-8859-1");
response.setHeader( "Content-Disposition", "attachment;filename=" + fileName);
java下载中文文件名乱码的解决方法
最新推荐文章于 2022-01-06 16:32:47 发布