gpt4 book ai didi

java - 支柱 : Download xls file with Arabic name doesn't work in Google Chrome

转载 作者:行者123 更新时间:2023-12-01 14:05:27 25 4
gpt4 key购买 nike

我尝试使用 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

最佳答案

new String(filename.getBytes("cp1256"), "cp1252") 当文件名包含 cp1252 中不可用的字符时,将会损坏文件名。即使没有,结果也只是偶然正确的。不要这样做。总是错的。

您可以在这个问题中找到解决方案:How to encode the filename parameter of Content-Disposition header in HTTP?

关于java - 支柱 : Download xls file with Arabic name doesn't work in Google Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18942482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com