chrome和firefox中的httpServletResponse行为

时间:2017-03-17 21:56:58

标签: java google-chrome firefox download

我有一个负责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中完成)并设置标题和内容类型,在谷歌浏览器中这完全有效但在firefox中文件被下载为&#34;文件&# 34;没有扩展名(但数据是正确的,好像我用excel打开文件都在那里)并且名称构建错误,因为在Chrome中这完全正常我认为在Firefox中响应的工作方式有问题,任何人我知道我可能做错了什么?

通过firefox image with files not working下载文件的示例 使用chrome enter image description here

下载的示例

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

根据RFC 6266,文件名value可以是tokenquoted-string

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

所以试试

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