(1)文件上传操作
public static final String FILE_LOCATION = "/home/huangwei/Desktop/";
public void upload(MultipartFile... files) {
for (MultipartFile file : files) {
String fileName = file.getOriginalFilename();
File uFile = new File(FILE_LOCATION + fileName);
try {
file.transferTo(uFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
(2) 文件下载操作
public ResponseEntity<byte[]> download(String fileName) {
ResponseEntity<byte[]> responseEntity = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(new File(FILE_LOCATION + fileName));
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment;fileName="+fileName);
responseEntity = new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
return responseEntity;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}