springmvc下载文件

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包

上一篇:二、JavaScript语言--JS基础--JavaScript入门篇


下一篇:iOS数据持久化--归档