如图的话
浏览器接收到了一堆东西 但是没有开启下载
一个简单而且符合springmvc方式的做法:
https://my.oschina.net/datevan/blog/654593
@RequestMapping("download")
public ResponseEntity<byte[]> download() throws IOException {
HttpHeaders headers = new HttpHeaders();
// headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//设置文件名
headers.setContentDispositionFormData("attachment", "a.xls");
String file = TestController.class.getResource("/config/").getPath()+"a.xls";
//把文件转成字节数组
File byteFile = new File(file);
int size = (int) byteFile.length();
FileInputStream inputStream = new FileInputStream(byteFile);
byte[] bytes = new byte[size];
int offset=0;
int readed;
while(offset<size && (readed = inputStream.read(bytes, offset,inputStream.available() )) != -1){
offset+=readed;
}
inputStream.close();
//返回
return new ResponseEntity<byte[]>(bytes,headers, HttpStatus.OK);
}