String fileName = "中国.doc";
//方法1:
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
//方法2:使用指定编码,并告诉浏览器编码类型
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + URLEncoder.encode(fileName, "UTF-8");
//方法3:
response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("gb2312"), "ISO-8859-1"));
本文介绍在HTTP响应中正确设置文件名编码的方法,包括使用UTF-8、GB2312和ISO-8859-1编码来确保不同浏览器能正确下载并显示中文文件名。方法1使用URLEncoder.encode直接编码;方法2指定编码类型;方法3转换编码。
5589





