..

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()));  
        }  

 

上传文件:

    @ApiOperation(value = "添加或者替换证书", notes = "1.添加后无需重启,若证书合法,则立即生效;\n" +
            "2.请确保上传的证书文件名称为license.lic,否则无效")
    @PostMapping("addLicense")
    public Json uploadLicense(MultipartFile file) {
        if (null == file) {
            return Json.fail("400", "获取上传文件失败,请检查file上传组件的名称是否正确");
        } else if (file.isEmpty()) {
            return Json.fail("400", "没有选择文件");
        } else {
            String filename = file.getOriginalFilename();
            filename = licenseStorePath + filename;
            File dest = new File(filename);
            try {
                file.transferTo(dest);
                app.publishEvent(new NeedLoadLicenseEvent(this));
                return Json.success("上传成功").data("filePath", filename);
            } catch (IOException e) {
                e.printStackTrace();
                return Json.fail("500", "文件上传发生异常").data("Exception", e.getLocalizedMessage());
            }
        }
    }

 

posted @ 2019-12-12 18:21  罗浩楠  阅读(343)  评论(0)    收藏  举报
..