Java文件上传与下载
文件上传
@GetMapping("upload")
public void upload(MultipartFile file) throws IOException {
String name = file.getName();
IOUtils.copy(file.getInputStream(), Files.newOutputStream(Paths.get("./" + name)));
}
文件下载
@GetMapping("download")
public void upload(HttpServletResponse response) throws IOException {
response.setContentType("multipart/form-data");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;fileName=test.jpg");
try(OutputStream stream = response.getOutputStream(); InputStream inputStream = Files.newInputStream(Paths.get("./test.jpg"))) {
IOUtils.copy(inputStream,stream);
}
}