1. String fileName=new String(URLEncoder.encode(fileName,"utf-8"));
getResponse().addHeader("Content-Disposition","attachment;filename="+fileName);
或者new String(fileName.getBytes(),"utf-8")
//都是乱码%E8%B5%84%E5%AE%A1%E6%96%87%E4%BB%B6%E6%A8%A1%E6%9D%BF
2.使用gbk呢,不管是URLEncoder.encode还是fileName.getBytes()都也是乱码 //如 ____.txt
3. 使用ISO8859-1呢
String fileName=new String(URLEncoder.encode(fileName,"ISO8859-1"));
//乱码 %3F%3F%3F%3F%3F%3F.txt
只有在
new String(fileName.getBytes(),"ISO8859-1"); //正确,不发生乱码
总之一句话,Java下载文件时文件名用new String(fileName.getBytes(),"ISO8859-1"); 转码就对了。