0

I tried to download xls file using Struts 1, it working in Firefox and downloaded the file with Arabic name but not working in Chrome.

Here is the code :

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");

    }

}

when I press the download link it downloads like this :

ØÇáÈÇÊ_ÇáÞÑÂä æÚáæãå.xls

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • possible duplicate of [How to encode the filename parameter of Content-Disposition header in HTTP?](http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http) – Aaron Digulla Sep 22 '13 at 10:17

1 Answers1

1

new String(filename.getBytes("cp1256"), "cp1252") will corrupt the file name when it contains characters that aren't available in cp1252. And even when it didn't, then the result is only correct by chance. Don't do this. It's always wrong.

You can find solutions for this in this question: How to encode the filename parameter of Content-Disposition header in HTTP?

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820