SpringBoot SpringMVC实现文件下载

作者: 光剑书架上的书 | 来源:发表于2017-08-24 14:28 被阅读727次

SpringBoot SpringMVC实现文件下载

   @RequestMapping(value = "/download", method = RequestMethod.GET)
    ResponseEntity<InputStreamResource> downloadFile(String log)
            throws IOException {
        String filePath = "/Users/alilang/logs/" + log
        FileSystemResource file = new FileSystemResource(filePath)
        HttpHeaders headers = new HttpHeaders()
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getFilename()))
        headers.add("Pragma", "no-cache")
        headers.add("Expires", "0")

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()))
    }

相关文章

网友评论

    本文标题:SpringBoot SpringMVC实现文件下载

    本文链接:https://www.haomeiwen.com/subject/nehedxtx.html