1

I am downloading a file from a controller. It does not work. It is because of the "," in the filename. If I remove the "," from the name it will work. Is there any work around for this. Should I do String.replace("," ""). There must be a better way?

response.setHeader("Content-Disposition", "inline; filename=" + "abbbbababbaababba.4.1.2013,aakdfhfdf.xlsx");

Complete method

@RequestMapping(value = "/newsimage/{newsImageId} ", method = RequestMethod.GET)
public void getImage(HttpServletRequest request, HttpServletResponse response, @PathVariable long newsImageId) {

    NewsImage newsImage = newsImageService.findById(newsImageId);
    String fileName = newsImage.getFileName();
        response.setHeader("Content-Disposition", "inline; filename=" + "abbbbababbaababba.4.1.2013,aakdfhfdf.xlsx");
    response.setContentType(newsImage.getContentType());
    // response.setHeader("Content-Disposition", "attachment;filename=" + newsImage.getFileName());

    OutputStream out;
    try {
        out = response.getOutputStream();
        out.write(newsImage.getData());
        out.flush();
    } catch (IOException e) {
        logger.error(e.getMessage());
    }

}
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
pethel
  • 5,397
  • 12
  • 55
  • 86
  • 2
    See also: http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http – Aaron Digulla Jan 09 '13 at 14:36

2 Answers2

6

You need to quote the filename, otherwise it's interpreted as part of header attribute.

response.setHeader("Content-Disposition", 
    "inline; filename=\"" + "abbbbababbaababba.4.1.2013,aakdfhfdf.xlsx" + "\"");
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

There are reserved characters and words but ',' is not one of them. Still I'd refrain from using ',' (replace with '-'). Also, '.' shouldn't be used for anything other than the suffix.

If you really really want to use ',' you should quote the entire file name, not because of operating system (or file system) restrictions but because of HTTP headers.

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198