Spring-mvc后台下载功能一种实现ResponseEntity

public ResponseEntity downloadFile() throws IOException {
   File file = fileService.getDownloadFile();//给回要下载的文件
   String destFileName = new String(
         file.getName().getBytes("gb2312"), "iso8859-1");//防止乱码
   //响应头,告诉浏览器
   HttpHeaders headers = new HttpHeaders();
   headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
   headers.setContentDispositionFormData("attachment", destFileName);

   return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
}

猜你喜欢

转载自blog.csdn.net/su_ren/article/details/82793899
今日推荐