【问题标题】:httpServletResponse behavior in chrome and firefoxchrome 和 firefox 中的 httpServletResponse 行为
【发布时间】:2017-08-09 14:40:27
【问题描述】:

我有一个负责下载 excel 的控制器方法:

@RequestMapping(value = "/report/user", method = RequestMethod.GET)
public void getUserReport(@RequestParam(value = "name", required = false) String user,
                          @RequestParam(value = "startdate", required = false) String startDate,
                          @RequestParam(value = "enddate", required = false) String endDate,
                          HttpServletResponse response) {

    List<Survey> userSurveys = surveyService.findUserSurveys(user, startDate, endDate);


    String userFileName = nameService.getUserFileName(user, startDate, endDate);
    Workbook userExcelReport = excelReportGenerator.createUserExcelReport(userSurveys);

    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-Disposition", "attachment; filename=" + userFileName + ".xlsx");


    try {
        userExcelReport.write(response.getOutputStream());
    } catch (IOException e) {
        logger.error("The excel workbook could not write to the outputStream ", e);
    }

}

如您所见,此控制器映射正在处理 excel 工作簿(在 apache poi 中完成)并设置标题和内容类型,在 google chrome 中这非常有效,但在 firefox 中,文件被下载为没有扩展名的“文件” (但是数据是正确的,就好像我用excel打开文件一样)并且名称构建错误,因为在chrome中这完全正常我认为在firefox中响应的工作方式有问题,任何人都知道什么我做错了吗?

通过firefox下载文件的示例 使用chrome下载示例

【问题讨论】:

  • 根据RFC 6266 filename valuetokenquoted-string。所以试试response.setHeader("Content-Disposition", "attachment; filename=\"" + userFileName + ".xlsx\"");
  • @AxelRichter 这成功了,愿意发帖作为答案让我接受吗?

标签: java google-chrome firefox download


【解决方案1】:

根据RFC 6266,文件名valuetokenquoted-string

token 不得包含 separators。但是空白是separator。所以你必须使用quoted-string

所以试试

response.setHeader("Content-Disposition", "attachment; filename=\"" + userFileName + ".xlsx\"");

【讨论】:

    【解决方案2】:

    请检查这种方式以发送内容类型作为响应。它对我有用。

    resp.setContentType( "application/octet-stream" );
    

    【讨论】:

      猜你喜欢
      • 2013-04-29
      • 2016-02-09
      • 2023-03-21
      • 2012-10-20
      • 2018-02-16
      • 2013-10-04
      • 2015-10-17
      • 2014-10-24
      • 2020-04-16
      相关资源
      最近更新 更多