0

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

4

1 回答 1

1

new String(filename.getBytes("cp1256"), "cp1252")当文件名包含cp1252. 即使没有,结果也只是偶然的。不要这样做。总是错的。

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

于 2013-09-22T10:17:32.967 回答