springmvc文件下载
在学习springmvc框架的时候遇到了文件下载,可以通过ResponseEntity来实现。
**
ResponseEntity
@RequestMapping("/download")
public ResponseEntity<byte[]> downLoad(String fileName, HttpServletRequest request) throws IOException {
String path=request.getSession().getServletContext().getRealPath("statics/uploadfiles/"+fileName);
File file =new File(path);
HttpHeaders httpHeader=new HttpHeaders();
//可以下载任意文件
httpHeader.setContentType(MediaType.APPLICATION_OCTET_STREAM);
httpHeader.setContentDispositionFormData("attachment",fileName);
return new ResponseEntity(FileUtils.readFileToByteArray(file),httpHeader, HttpStatus.OK);
}
**
只要前端页面传一个文件名就能实现下载