springboot#下载文件

膜拜大神

这就是我要的滑板鞋!

    @RequestMapping(value = "/media", method = RequestMethod.GET)  
        public ResponseEntity<InputStreamResource> downloadFile( Long id)  
                throws IOException {  
            String filePath = "E:/" + id + ".rmvb";  
            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()));  
        }  

猜你喜欢

转载自www.cnblogs.com/luohaonan/p/12030883.html