上传
@RequestMapping("/upload")
public static String upload(@RequestPart(value = "resource") MultipartFile file){
String path ="C:/Users/hp/Desktop";
String fileLastType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
String uuid = UUID.randomUUID().toString();
String realPath = path + "/" + uuid + fileLastType;
File dest = new File(realPath);
if(!dest.getParentFile().exists()){
dest.getParentFile().mkdir();
}
try {
file.transferTo(dest);
return uuid + fileLastType;
} catch (Exception e) {
log.error(e.toString());
return "error";
}
}
下载
@RequestMapping("/download")
public void download(HttpServletResponse response) throws IOException {
String filename = "testfile.txt";
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
InputStream fis = new FileInputStream(new File("C:/Users/hp/Desktop/xx.txt"));
ServletOutputStream outputStream = response.getOutputStream();
byte[] data = new byte[2048];
int len = -1;
while ((len = fis.read(data)) != -1) {
outputStream.write(data, 0, len);
}
outputStream.flush();
outputStream.close();
fis.close();
}