if (null != agent && -1 != agent.indexOf("Mozilla")){//firefoxjava下载文件时的文件名乱码处理

java下载文件时的文件名乱码处理

  在不同浏览器下需要有不同的处理,否则还是会出现文件名为乱码:

//判断浏览器

}


,ie是一样的,火狐不同些,谷歌兼容两种



String agent = request.getHeader("USER-AGENT");

if (null != agent && -1 != agent.indexOf("Mozilla")){//firefox
response.setHeader("Content-Disposition", "attachment;filename=\""
+ new String(file.getName().getBytes("UTF-8"),"iso-8859-1")
+ "\"");
}else{//IE,其他MSIE,
// 设置文件头,文件名称或编码格式
response.setHeader("Content-Disposition", "attachment;filename=\""
+ java.net.URLEncoder.encode(file.getName(), "UTF-8")
+ "\"");
}

你可能感兴趣的:(java,浏览器,IE,firefox)