• springboot 实现文件下载功能


    文件存在在data目录下

    @GetMapping(value = "/file/download")
        public ResponseEntity<FileSystemResource> getFile(@RequestParam String fileName) throws FileNotFoundException {
            String path = System.getProperty("user.dir")+ "/data/";
            File file = new File(path + fileName);
            if (file.exists()) {
                return export(file);
            }
            System.out.println(file);
            return null;
        }
    
    
        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=" + file.getName());
            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));
        }
  • 相关阅读:
    数字麦克风PDM信号采集与STM32 I2S接口应用(四)--单片机源码
    Golang SQL连接池梳理
    Ghost-无损DDL
    蛮好用的网站
    齿轮
    water
    折纸 (模拟)
    不等式(数学)
    周期串查询
    大集训模拟赛十一
  • 原文地址:https://www.cnblogs.com/leavescy/p/12845482.html
Copyright © 2020-2023  润新知