1

我正在从控制器下载文件。这没用。这是因为文件名中的“,”。如果我从名称中删除“,”,它将起作用。是否有任何解决方法。我应该做 String.replace("," "")。肯定有更好的办法?

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

完整方法

@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());
    }

}
4

2 回答 2

6

您需要引用文件名,否则它会被解释为 header 属性的一部分。

response.setHeader("Content-Disposition", 
    "inline; filename=\"" + "abbbbababbaababba.4.1.2013,aakdfhfdf.xlsx" + "\"");
于 2013-01-09T14:34:39.037 回答
2

保留字符和单词,但“,”不是其中之一。我仍然会避免使用“,”(替换为“-”)。还, '。' 不应用于后缀以外的任何内容。

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.

于 2013-01-09T14:34:42.803 回答