【问题标题】:"," in file name when downloading file下载文件时文件名中的“,”
【发布时间】:2013-01-09 14:31:04
【问题描述】:

我正在从控制器下载文件。这没用。这是因为文件名中的“,”。如果我从名称中删除“,”,它将起作用。是否有任何解决方法。我应该做 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());
    }

}

【问题讨论】:

标签: java spring servlets response


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    reserved characters and words,但 ',' 不是其中之一。我仍然会避免使用“,”(替换为“-”)。还, '。'不应用于后缀以外的任何内容。

    如果你真的想使用 ',' 你应该引用整个文件名,不是因为操作系统(或文件系统)的限制,而是因为 HTTP 标头。

    【讨论】:

      猜你喜欢
      • 2019-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      相关资源
      最近更新 更多