• 使用ResponseEntity实现文件下载


    使用ResponseEntity实现对jar内部和外部的数据文件实现下载。
    ResponseEntity的泛型可以设置自定义返回值,也可以使用自带的返回值。

    
    
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.InputStreamResource;
    import org.springframework.core.io.Resource;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    @RestController
    @RequestMapping("/test")
    public class TestController {
    
        // 设置返回值
        class MyResult<T>{
            private int code;
            private String msg;
            private T data;
    
            public int getCode() {
                return code;
            }
    
            public void setCode(int code) {
                this.code = code;
            }
    
            public String getMsg() {
                return msg;
            }
    
            public void setMsg(String msg) {
                this.msg = msg;
            }
    
            public T getData() {
                return data;
            }
    
            public void setData(T data) {
                this.data = data;
            }
        }
    
        @PostMapping("/download/out")
        public ResponseEntity<Object> downloadOut(){
    
            try {
                // Set the file name
                String fileName = "my.xlsx";
    
                // 工程的目录
                String prefixPath = System.getProperty("user.dir");
                // 文件路径
                String filePath = "/data/"+fileName;
    
                // 设置头信息
                HttpHeaders headers = new HttpHeaders();
                // 设置文件名称
                headers.setContentDispositionFormData("attachment", fileName);
    
                // 转化文件
                Path path = Paths.get(prefixPath + "/" + filePath);
                Resource resource = new InputStreamResource(Files.newInputStream(path));
    
                // 设置返回值
                return ResponseEntity.ok()
                        .headers(headers)
                        .contentLength(Files.size(path))
                        .contentType(MediaType.APPLICATION_OCTET_STREAM)
                        .body(resource);
            } catch (IOException e) {
                System.out.println("下载文件失败");
    
                MyResult<String> result = new MyResult<>();
                result.setCode(-1);
                result.setMsg("下载文件失败");
    
                // 返回实体值
                return ResponseEntity.ok(result);
            }
        }
    
        @PostMapping("/download/in")
        public ResponseEntity<Object> downloadIn(){
    
            System.out.println("test");
            try {
                // Set the file name
                String fileName = "my.xlsx";
    
                // 文件路径
                String filePath = "/data/"+fileName;
    
                // 设置头信息
                HttpHeaders headers = new HttpHeaders();
                // 设置文件名称
                headers.setContentDispositionFormData("attachment", fileName);
    
                System.out.println(filePath);
    
                // 获取资源jar包下的数据文件
                ClassPathResource classPathResource = new ClassPathResource(filePath);
                Resource resource = new InputStreamResource(classPathResource.getInputStream());
    
                // 设置返回值
                return ResponseEntity.ok()
                        .headers(headers)
                        .contentLength(classPathResource.contentLength())
                        .contentType(MediaType.APPLICATION_OCTET_STREAM)
                        .body(resource);
            } catch (IOException e) {
                System.out.println("下载文件失败");
    
                MyResult<String> result = new MyResult<>();
                result.setCode(-2);
                result.setMsg("下载文件失败");
    
                // 返回实体值
                return ResponseEntity.ok(result);
            }
    
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
  • 相关阅读:
    机器人工具箱学习(三)
    [云原生案例2.1 ] Kubernetes的部署安装 【单master集群架构 ---- (二进制安装部署)】节点部分
    《windows核心编程》第1章 错误处理
    LeetCode(14)加油站【数组/字符串】【中等】
    SSL证书续费相关问题详解
    初级C语言之【函数】
    蓝蜂物联网水肥一体化MQTT应用案例
    获取真实IP总结
    《Mycat分布式数据库架构》之全局自增ID
    Redis——》单线程还是多线程
  • 原文地址:https://blog.csdn.net/make_progress/article/details/133124226