HttpServletResponse.addHeader()和setHeader()在Spring Controller中不起作用

时间:2014-05-07 18:06:08

标签: spring spring-mvc servlets httpresponse

我尝试使用spring-mvc,spring-boot和spring security构建一个小型Web应用程序。只使用一个控制器,其中一个服务端点是让用户下载Web应用程序生成的docx文件。我的逻辑代码运行良好,问题是当我想将Headers添加到HttpServletResponse时,addHeader()和setHeader()不起作用,我只想指定下载文件的名称。我打印了一些日志,并且不知道为什么这不起作用。

以下是我的控制器的部分代码:

@Controller
public class ImportExportController {

    private final static Logger LOGGER = LoggerFactory.getLogger(ImportExportController.class);

    @Autowired
    private WordProcessor wordProcessor;

    @RequestMapping("/export")
    public void export(@RequestParam(value = "domainName", required = true) String domainName,
                       @RequestParam(value = "projectName", required = true) String projectName,
                       @RequestParam(value = "testFolderId", required = true) int testFolderId,
                       HttpServletRequest request, HttpServletResponse response) {

        String exportedFileName = "exportedTC_" + domainName + "_" + projectName + "_"
                + Integer.toString(testFolderId) + ".docx";

        try {
            extendExpiredALMSession();
            SaveToZipFile saver = wordProcessor.ExportToWord(domainName, projectName,
                                                             Integer.toString(testFolderId));
            saver.save(response.getOutputStream());
            response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            LOGGER.info("exportedFileName: " + exportedFileName);
            LOGGER.info("contains: " + response.containsHeader("Content-Disposition"));
            response.addHeader("Content-Disposition", "attachment; filename=\"" + exportedFileName + "\"");
            for (String name : response.getHeaderNames()) {
                LOGGER.info("Header: " + name);
            }
            LOGGER.info("Date Header:" + response.getHeader("Date"));
            LOGGER.info("Content-Disposition header: " + response.getHeader("Content-Disposition"));
            LOGGER.info("ContentType: " + response.getHeader("ContentType"));
            response.flushBuffer();
        } catch (RequestFailureException | RESTAPIException | InvalidDataException | UnLoginException
                | UnAuthorizedOperationException | IOException | Docx4JException | URISyntaxException e) {
            e.printStackTrace();
        }
    }
}

这是我得到的日志,你可以看到Header" Content-Disposition"和" ContentType"都是空的。

2014-05-07_13:35:05.646 INFO  c.c.p.a.w.w.ImportExportController - exportedFileName: exportedTC_DEFAULT_JIRA_Test_CPL5_4520.docx
2014-05-07_13:35:05.646 INFO  c.c.p.a.w.w.ImportExportController - contains: false
2014-05-07_13:35:05.646 INFO  c.c.p.a.w.w.ImportExportController - Header: X-Content-Type-Options
2014-05-07_13:35:05.646 INFO  c.c.p.a.w.w.ImportExportController - Header: X-XSS-Protection
2014-05-07_13:35:05.646 INFO  c.c.p.a.w.w.ImportExportController - Header: Cache-Control
2014-05-07_13:35:05.647 INFO  c.c.p.a.w.w.ImportExportController - Header: Pragma
2014-05-07_13:35:05.647 INFO  c.c.p.a.w.w.ImportExportController - Header: Expires
2014-05-07_13:35:05.647 INFO  c.c.p.a.w.w.ImportExportController - Header: X-Frame-Options
2014-05-07_13:35:05.647 INFO  c.c.p.a.w.w.ImportExportController - Header: X-Application-Context
2014-05-07_13:35:05.647 INFO  c.c.p.a.w.w.ImportExportController - Header: Transfer-Encoding
2014-05-07_13:35:05.647 INFO  c.c.p.a.w.w.ImportExportController - Header: Date
2014-05-07_13:35:05.647 INFO  c.c.p.a.w.w.ImportExportController - Date Header:Wed, 07 May 2014 17:35:05 GMT
2014-05-07_13:35:05.647 INFO  c.c.p.a.w.w.ImportExportController - Content-Disposition header: null
2014-05-07_13:35:05.647 INFO  c.c.p.a.w.w.ImportExportController - ContentType: null

感谢阅读。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:6)

今天我也遇到了类似于此处描述的完全相同的问题。我已经调整了一点,在this tutorial中发现必须在内容之前设置标题。然后我换了线,一切都很好,就像一个魅力。

在你的情况下,我会建议推线 <{1}}之前saver.save(response.getOutputStream());已经设置了所有标题之后。

答案 1 :(得分:2)

事实证明,Spring Controller具有所有响应的默认标头,因此我可以访问响应主体但不能访问标头。要设置HttpHeaders,返回一个HttpEntity就行了。解决方案代码如下:

  @RequestMapping(value = "/export", method = RequestMethod.GET, produces = "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
    public HttpEntity<byte[]> export(@RequestParam(value = "domainName", required = true) String domainName,
                                     @RequestParam(value = "projectName", required = true) String projectName,
                                     @RequestParam(value = "testFolderId", required = true) int testFolderId) {

    String exportedFileName = "exportedTC_" + domainName + "_" + projectName + "_"
            + Integer.toString(testFolderId) + ".docx";
    SaveToZipFile saver = null;
    ByteArrayOutputStream out = null;
    HttpHeaders responseHeaders = null;
    byte[] documentBody = null;
    try {
        extendExpiredALMSession();
        saver = wordProcessor.ExportToWord(domainName, projectName, Integer.toString(testFolderId));
        out = new ByteArrayOutputStream();
        saver.save(out);
        responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type",
                            "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        responseHeaders.add("Content-Disposition", "attachment; filename=\"" + exportedFileName + "\"");
        documentBody = out.toByteArray();
    } catch (RequestFailureException | RESTAPIException | InvalidDataException | UnLoginException
            | UnAuthorizedOperationException | IOException | Docx4JException | URISyntaxException e) {
        e.printStackTrace();
    }
    return new HttpEntity<byte[]>(documentBody, responseHeaders);
    }

这适合我。