19. 文件上传下载
上传
@RequestMapping("/upload")
public void ss(MultipartFile file){
File path = null;
try {
path = new File(ResourceUtils.getURL("classpath:").getPath());
File upload = new File(path.getAbsolutePath(),"static/img/");
if(!upload.exists()){
upload.mkdirs();
}
File fileUp = new File(upload.getAbsolutePath()+ "/"+file.getOriginalFilename());
file.transferTo(fileUp);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
下载
@RequestMapping(path = "/download", method = RequestMethod.GET)
public ResponseEntity<Resource> download(String param) throws IOException {
// ...
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
return ResponseEntity.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
}