火狐浏览器下载时名称为乱码,怎么解决? 这个我知道啊,我设置的编码方式是UTF-8,但就是在火狐浏览器上行不通 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 自己写的servlet还是怎么。如果是自己写的servlet下载的话加上这个试试看response.setHeader("Content-Disposition", "attachment; filename=\""+URLEncoder.encode("//文件名", "utf-8//編碼方式")+"\""); 这是我的:response.addHeader("Content-Disposition","attachment; filename="+ URLEncoder.encode(fileName, "UTF-8")); response.setCharacterEncoding("UTF-8");response.setContentType("text/xml这里天MIME类型; charset=UTF-8");试试看,好像csdn用火狐下载也是乱码 我也这么写了,也加了“charset=UTF-8”,还是没用 在火狐里打about:config找找看有关字符编码的设置看看吧,要是还不行,就无能为力了 http://apps.hi.baidu.com/share/detail/21304246嗯,试试这个看看 最后还是被我自己解决了,先在页面做下判断,判断是什么浏览器,然后传一个参数到servlet,进行用不同的编码方式:火狐的:response.addHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("GB2312"),"ISO-8859-1"));IE及其他:response.addHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName, "UTF-8")); //判断当前使用浏览器版本类 var SysBrowser = function(){ this.ua = navigator.userAgent.toLowerCase(); this.browserAndVersion; this.verIE ; this.verFirefox; this.verChrome; this.verOpera; this.verSafari; this.getBrowser = function(){ (this.browserAndVersion = this.ua.match(/msie ([\d.]+)/)) ? this.verIE = parseInt(this.browserAndVersion[1],10) : (this.browserAndVersion = this.ua.match(/firefox\/([\d.]+)/)) ? this.verFirefox = this.browserAndVersion[1] : (this.browserAndVersion = this.ua.match(/chrome\/([\d.]+)/)) ? this.verChrome = this.browserAndVersion[1] : (this.browserAndVersion = this.ua.match(/opera.([\d.]+)/)) ? this.verOpera = this.browserAndVersion[1] : (this.browserAndVersion = this.ua.match(/version\/([\d.]+).*safari/)) ? this.verSafari = this.browserAndVersion[1] : 0; //以下进行测试 if (this.verIE) return "IE"+this.verIE; if (this.verFirefox) return "Firefox"; if (this.verChrome) return "Chrome"; if (this.verOpera) return "Opera"; if (this.verSafari) return "Safari"; } }; 后台是 C#的话,可以这样弄 Response.Clear(); string UserAgent = Request.ServerVariables["http_user_agent"].ToLower(); string FileName = "附件.rar"; if (UserAgent.IndexOf("firefox") == -1) {//非火狐浏览器 FileName = HttpUtility.UrlEncode(FileName, Encoding.UTF8); } string Type = "application/zip"; Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName); Response.AddHeader("Content-Length ", suppliers.Attachment.Length.ToString()); Response.ContentType = Type; Response.ContentEncoding = Encoding.UTF8; Response.BinaryWrite(suppliers.Attachment); Response.End(); suppliers.Attachment 为 Byte[] 文件流 //火狐 if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) { fileNameTemp = new String(fileNameTemp.getBytes("UTF-8"), "ISO8859-1"); } //IE else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0){ fileNameTemp = URLEncoder.encode(fileNameTemp, "UTF-8"); } response.setHeader("Content-Disposition", "attachment;filename=\""+fileNameTemp+"\""); 刚学jsee,上面好多代码都看不懂。public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获得客户端提交 下载文件绝对路径 String path = request.getParameter("path");// GET提交中文乱码 path = new String(path.getBytes("ISO-8859-1"), "utf-8");// 手动解决乱码 String filename = path.substring(path.lastIndexOf("\\") + 1);// 文件名 // 设置文件类型 response.setContentType(getServletContext().getMimeType(filename)); // 设置Content-Disposition String agent = request.getHeader("user-agent"); System.out.println(agent); if (agent.contains("MSIE")) { // IE浏览器 --- URL编码 filename = URLEncoder.encode(filename, "utf-8"); } else if (agent.contains("Firefox")) { // 火狐浏览器 --- Base64编码 filename = base64EncodeFileName(filename); } else { filename = URLEncoder.encode(filename, "utf-8"); } response.setHeader("Content-Disposition", "attachment;filename=" + filename); // 将文件内容写入客户端 InputStream in = new BufferedInputStream(new FileInputStream(path)); OutputStream out = new BufferedOutputStream(response.getOutputStream()); int temp; while ((temp = in.read()) != -1) { out.write(temp); } in.close(); out.close(); } public static String base64EncodeFileName(String fileName) { BASE64Encoder base64Encoder = new BASE64Encoder(); try { return "=?UTF-8?B?" + new String(base64Encoder.encode(fileName .getBytes("UTF-8"))) + "?="; } catch (UnsupportedEncodingException e) { e.printStackTrace(); throw new RuntimeException(e); } }火狐要用base64解决编码问题 学习了!但是我想说你确定:response.addHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("GB2312"),"ISO-8859-1"));这句能编译通过?我找到一个方法:string outputFileName = null; outputFileName = "\"" + FileName+"\"";Response . AddHeader("Content-Disposition" , "attachment;filename=" + outputFileName); String agent = req.getHeader("user-agent"); resp.setContentType("application/x-download"); if(agent.contains("Firefox")) resp.addHeader("Content-Disposition","attachment;filename="+ new String(displayFileName.getBytes("GB2312"),"ISO-8859-1")); else resp.addHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(displayFileName, "UTF-8"));刚刚我也遇到了这个问题,综合上面的回答,发下我的做法,在IE,谷歌,火狐等浏览器测试没问题! 各位大神,不吝赐教 jdbc的小问题 求高人简答这道简单的题目。 FCK过滤标签等问题 spring配置问题 (两问题)网站的优化,搜索引擎技术 高分求救,怎样提取字符串中的数据 高手请进 做.NET快两年了,现在公司决策,把NET部门,转到JAVA部门,做JAVA去,兄弟们,给点意见,合适吗 求助:ejb sessionBean的事务问题 spring mvc中如何获取Response对象 [求教]返回在两字符串中第一个相同的子串(大于1个字符),并将子串输出
response.setHeader("Content-Disposition", "attachment; filename=\""+URLEncoder.encode("//文件名", "utf-8//編碼方式")+"\"");
response.addHeader("Content-Disposition","attachment; filename="+ URLEncoder.encode(fileName, "UTF-8"));
response.setContentType("text/xml这里天MIME类型; charset=UTF-8");试试看,好像csdn用火狐下载也是乱码
嗯,试试这个看看
火狐的:
response.addHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("GB2312"),"ISO-8859-1"));
IE及其他:
response.addHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName, "UTF-8"));
var SysBrowser = function(){
this.ua = navigator.userAgent.toLowerCase();
this.browserAndVersion;
this.verIE ;
this.verFirefox;
this.verChrome;
this.verOpera;
this.verSafari;
this.getBrowser = function(){
(this.browserAndVersion = this.ua.match(/msie ([\d.]+)/)) ? this.verIE = parseInt(this.browserAndVersion[1],10) :
(this.browserAndVersion = this.ua.match(/firefox\/([\d.]+)/)) ? this.verFirefox = this.browserAndVersion[1] :
(this.browserAndVersion = this.ua.match(/chrome\/([\d.]+)/)) ? this.verChrome = this.browserAndVersion[1] :
(this.browserAndVersion = this.ua.match(/opera.([\d.]+)/)) ? this.verOpera = this.browserAndVersion[1] :
(this.browserAndVersion = this.ua.match(/version\/([\d.]+).*safari/)) ? this.verSafari = this.browserAndVersion[1] : 0;
//以下进行测试
if (this.verIE) return "IE"+this.verIE;
if (this.verFirefox) return "Firefox";
if (this.verChrome) return "Chrome";
if (this.verOpera) return "Opera";
if (this.verSafari) return "Safari";
}
};
string UserAgent = Request.ServerVariables["http_user_agent"].ToLower();
string FileName = "附件.rar";
if (UserAgent.IndexOf("firefox") == -1)
{//非火狐浏览器
FileName = HttpUtility.UrlEncode(FileName, Encoding.UTF8);
}
string Type = "application/zip";
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.AddHeader("Content-Length ", suppliers.Attachment.Length.ToString());
Response.ContentType = Type;
Response.ContentEncoding = Encoding.UTF8;
Response.BinaryWrite(suppliers.Attachment);
Response.End();
//火狐
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
fileNameTemp = new String(fileNameTemp.getBytes("UTF-8"), "ISO8859-1");
}
//IE
else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0){
fileNameTemp = URLEncoder.encode(fileNameTemp, "UTF-8");
}
response.setHeader("Content-Disposition", "attachment;filename=\""+fileNameTemp+"\"");
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获得客户端提交 下载文件绝对路径
String path = request.getParameter("path");// GET提交中文乱码
path = new String(path.getBytes("ISO-8859-1"), "utf-8");// 手动解决乱码 String filename = path.substring(path.lastIndexOf("\\") + 1);// 文件名
// 设置文件类型
response.setContentType(getServletContext().getMimeType(filename));
// 设置Content-Disposition
String agent = request.getHeader("user-agent");
System.out.println(agent);
if (agent.contains("MSIE")) {
// IE浏览器 --- URL编码
filename = URLEncoder.encode(filename, "utf-8");
} else if (agent.contains("Firefox")) {
// 火狐浏览器 --- Base64编码
filename = base64EncodeFileName(filename);
} else {
filename = URLEncoder.encode(filename, "utf-8");
}
response.setHeader("Content-Disposition", "attachment;filename="
+ filename); // 将文件内容写入客户端
InputStream in = new BufferedInputStream(new FileInputStream(path));
OutputStream out = new BufferedOutputStream(response.getOutputStream());
int temp;
while ((temp = in.read()) != -1) {
out.write(temp);
}
in.close();
out.close();
} public static String base64EncodeFileName(String fileName) {
BASE64Encoder base64Encoder = new BASE64Encoder();
try {
return "=?UTF-8?B?"
+ new String(base64Encoder.encode(fileName
.getBytes("UTF-8"))) + "?=";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
火狐要用base64解决编码问题
这句能编译通过?我找到一个方法:
string outputFileName = null;
outputFileName = "\"" + FileName+"\"";
Response . AddHeader("Content-Disposition" , "attachment;filename=" + outputFileName);
resp.setContentType("application/x-download");
if(agent.contains("Firefox"))
resp.addHeader("Content-Disposition","attachment;filename="+ new String(displayFileName.getBytes("GB2312"),"ISO-8859-1"));
else
resp.addHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(displayFileName, "UTF-8"));刚刚我也遇到了这个问题,综合上面的回答,发下我的做法,在IE,谷歌,火狐等浏览器测试没问题!