通过Spring Web下载文件

时间:2017-01-19 18:25:52

标签: java spring file download

目前,这是我在控制器中的代码,用于返回XLS文件以供下载:

@RequestMapping(value = "/export-data/", method = RequestMethod.GET)
public ResponseEntity exportAllData() {
        ResponseEntity respEntity = null;
        SheetDownload sheetDownload = new SheetDownload();

        try {
            ByteArrayOutputStream result = sheetDownload.createMentoringSheet();

            HttpHeaders responseHeaders = new HttpHeaders();

            byte[] out = result.toByteArray();

            responseHeaders.add("content-disposition", "attachment; filename=export-data.xlsx");
            responseHeaders.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

            respEntity = new ResponseEntity(out, responseHeaders,HttpStatus.OK);
        }catch(Exception e){
            respEntity = new ResponseEntity ("File Not Found", HttpStatus.OK);
        }

        return respEntity;
}

当我去"网络"在Chrome中,我在回复中看到的只有: enter image description here

因此,例如,浏览器下载没有触发器。目标是以适当的格式(XLSX)返回要下载的文件。

有人能帮助我吗?

提前谢谢。

0 个答案:

没有答案