上传
后端
@RequestMapping("upload")
public String upload(MultipartFile file) throws IOException {
file.transferTo(new File("D:\\d"+file.getOriginalFilename()));
return "success";
}
前端
<h6>文件上传</h6>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="SUBMIT">
</form>
配置
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
id="multipartResolver">
<property name="maxUploadSize" value="307200"/>
</bean>
下载
后端
@GetMapping("download")
public ResponseEntity<byte[]> download() throws IOException {
File file = new File("D:\\tmp\\day02.docx");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentDispositionFormData("attachment", "day02.docx");
httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), httpHeaders, HttpStatus.OK);
}