通过接口实现下载文件

直接上代码

    /**
     * 通过接口下载文件
     *
     * @param file 文件
     * @return ResponseEntity
     */
    public ResponseEntity<FileSystemResource> export(File file) {
    
    
        if (file == null) {
    
    
            return null;
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", "attachment; filename=" + System.currentTimeMillis() + ".txt");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        headers.add("Last-Modified", new Date().toString());
        headers.add("ETag", String.valueOf(System.currentTimeMillis()));

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

Guess you like

Origin blog.csdn.net/haohao_ding/article/details/111994591