springmvc下载文件

时间:2022-12-08 19:39:38

Controller内代码:

@RequestMapping(value = "/upload")
public ResponseEntity<byte[]> upload() throws IOException{
File file=new File("G:\\e盘\\恒之工作室\\书\\zxc.pdf");
HttpHeaders headers = new HttpHeaders();
String fileName=new String("zxc.pdf".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}

需要用到commons-io-XX.jar包