Spring MVC實現文件下載

 下載文件①java

 

下載文件須要將byte數組還原成文件。spring

         首先使用mybatis將數據庫中的byte數組查出來,指定文件名(包括格式)。而後使用OutputStream將文件輸入數據庫

 

  1. @RequestMapping(value = "downPhotoById")  
  2. public void downPhotoByStudentId(String id, final HttpServletResponse response){  
  3.     PhotoEntity entity = this.photoMapper.getPhotoEntityByPhotoId(id);  
  4.     byte[] data = entity.getPhotoData();  
  5.     String fileName = entity.getFileName()== null ? "照片.png" : entity.getFileName();  
  6.     fileName = URLEncoder.encode(fileName, "UTF-8");  
  7.     response.reset();  
  8.     response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");  
  9.     response.addHeader("Content-Length", "" + data.length);  
  10.     response.setContentType("application/octet-stream;charset=UTF-8");  
  11.     OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());  
  12.     outputStream.write(data);  
  13.     outputStream.flush();  
  14.     outputStream.close();  
  15. }  

 

  1. <a href="<%=request.getContextPath() %>/downPhotoById.do?id=8000001">下載照片</a>  

 

 下載文件②數組

 

/** * @Description 下載文件 mybatis

* @author jxldjsnmvc

* @date 2015年12月11日 下午6:11:33 app

* @param fileName this

* @param file spa

* @return code

* @throws IOException

*/

public ResponseEntity<byte[]> download(String fileName, File file) throws IOException {

String dfileName = new String(fileName.getBytes("gb2312"), "iso8859-1");

HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", dfileName);

return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); }

 下載文件③

//文件下載 主要方法
 public static void download(HttpServletRequest request,    HttpServletResponse response, String storeName, String contentType   ) throws Exception {     request.setCharacterEncoding("UTF-8");   BufferedInputStream bis = null;   BufferedOutputStream bos = null;   //獲取項目根目錄   String ctxPath = request.getSession().getServletContext()     .getRealPath("");     //獲取下載文件露肩   String downLoadPath = ctxPath+"/uploadFile/"+ storeName;   //獲取文件的長度   long fileLength = new File(downLoadPath).length();   //設置文件輸出類型   response.setContentType("application/octet-stream");   response.setHeader("Content-disposition", "attachment; filename="     + new String(storeName.getBytes("utf-8"), "ISO8859-1"));   //設置輸出長度   response.setHeader("Content-Length", String.valueOf(fileLength));   //獲取輸入流   bis = new BufferedInputStream(new FileInputStream(downLoadPath));   //輸出流   bos = new BufferedOutputStream(response.getOutputStream());   byte[] buff = new byte[2048];   int bytesRead;   while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {    bos.write(buff, 0, bytesRead);   }   //關閉流   bis.close();   bos.close();  }  }


下載直接訪問控制器如:http:\\localhost:8080/springmvc/download.do


 下載文件④

  1. @RequestMapping("/export")  
  2.     public ResponseEntity<byte[]> export() throws IOException {  
  3.         HttpHeaders headers = new HttpHeaders();    
  4.         headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);    
  5.         headers.setContentDispositionFormData("attachment", "dict.txt");    
  6.         return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File("C:/Users/Administrator/Desktop/a.txt")),    
  7.                                               headers, HttpStatus.CREATED);    
  8.     }  
相關文章
相關標籤/搜索