基于springboot的文件上传下载
文件上传
@PostMapping("/fileUpLoad")
public Map fileUpLoad(@RequestParam("file") MultipartFile file) throws IOException {
String fileName = file.getOriginalFilename();
System.out.println("文件上传,入参为:"+fileName);
String fileUrl="D:\\Documents\\Downloads\\"+fileName;
file.transferTo(new File(fileUrl));
Map map = new HashMap();
map.put("code",200);
map.put("msg","文件上传success");
return map;
}
文件下载
@GetMapping("/fileDownLoad")
public ResponseEntity<byte[]> fileDownLoad() throws IOException {
System.out.println("执行文件下载");
FileInputStream fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\demo.zip");
byte[] body = new byte[fis.available()];
fis.read(body);
HttpHeaders header = new HttpHeaders();
header.add("Content-Disposition","attachment;filename=demo.zip");
ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(body,header, HttpStatus.OK);
return responseEntity;
}