我尝试使用Struts 1下载xls文件,它在Firefox中运行,并使用阿拉伯名称下载该文件,但无法在Chrome中运行。
以下是代码:
public void download(CommandContext context, byte[] data, String filename) {
String CONTENT_TYPE = "application/unknown";
HttpServletResponse response = (HttpServletResponse) context
.get(context.RESPONSE_KEY);
try {
response.setContentType(CONTENT_TYPE);
response.setContentLength(data.length);
String str = "attachment; filename=\""
+ new String(filename.getBytes("cp1256"), "cp1252") + "\"";
response.setHeader("Content-Disposition", str);
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
OutputStream outputStream = response.getOutputStream();
outputStream.write(data);
outputStream.flush();
outputStream.close();
} catch (IOException ex) {
getLogger(this.getClass().getName()).error(
"Can't download file of newly generated card numbers");
}
}
当我按下载链接时,它会像这样下载:
ØÇáÈÇÊ_ÇáÞÑÂäæÚáæãå.xls
答案 0 :(得分:1)
new String(filename.getBytes("cp1256"), "cp1252")
中不可用的字符, cp1252
将损坏文件名。即使它没有,那么结果只是偶然的。不要这样做。这总是错的。
您可以在此问题中找到解决方案:How to encode the filename parameter of Content-Disposition header in HTTP?