81,092
社区成员
发帖
与我相关
我的任务
分享
@GetMapping("/download")
@ResponseBody
public ResponseEntity<byte[]> download() throws IOException {
File file=new File("文件的绝对路径");
HttpHeaders headers = new HttpHeaders();
String fileName=new String(("文件名"+".tar").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); //向浏览器发送数据
}
本来是这个问题,已经写出来了