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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章