1. 程式人生 > >springmvc下載檔案以及下載大檔案方法

springmvc下載檔案以及下載大檔案方法

後臺springmvc方法: 

@RequestMapping(value = "/down", method = RequestMethod.GET) 

public ResponseEntity download(String type,HttpSession session)
throws IOException {
String filename="";
switch(type){
case "1d":filename="一手樓盤操作說明.docx";break;
case "1m":filename="一手樓盤管理.mp4";break;
case "2d":filename="二手房源操作說明.docx";break;
case "2m":filename="二手房源列表管理.mp4";break;
case "2km":filename="二手樓盤房源管理.mp4";break;
case "xd":filename="小區管理操作說明.docx";break;
case "xm":filename="小區資訊管理.mp4";break;
case "mm":filename="我釋出的房源.mp4";break;
case "sm":filename="系統管理.mp4";break;
default:break;
}
   File file = new File(session.getServletContext().getRealPath("/")+"attachment/"+filename);
   String dfileName = new String(file.getName().getBytes("gb2312"), "iso8859-1");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", dfileName);
return new ResponseEntity(FileUtils.readFileToByteArray(file), headers,
HttpStatus.CREATED);
}


前端用a連線:

<a id="edit"   href="${baseURL}/downfile/down?type=2m" style="margin-left: 170px;">
                <i class="glyphicon glyphicon-edit"></i> 二手房源列表操作視訊
            </a>

springmvc提供的簡單的下載功能,這種有侷限性,當檔案資料過大的時候(大概超過40M左右)就會容易記憶體溢位,因為這種方式是:FileUtils.readFileToByteArray(file),一次性讀取檔案的,如果檔案過大,可以考慮分批讀取。下面提供分批讀取的方法,這種可以解決檔案過大下載記憶體溢位的問題:

 try {   
   File file = new File(session.getServletContext().getRealPath("/")+"attachment/"+filename);
             if (file.exists()) {   
                String dfileName = file.getName();   
                InputStream fis = new BufferedInputStream(new FileInputStream( file));   
                 response.reset();   
                 response.setContentType("application/x-download");
                 response.addHeader("Content-Disposition","attachment;filename="+ new String(dfileName.getBytes(),"iso-8859-1"));
                 response.addHeader("Content-Length", "" + file.length());   
                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());   
               response.setContentType("application/octet-stream");   
                 byte[] buffer = new byte[1024 * 1024 * 4];   
                 int i = -1;   
                 while ((i = fis.read(buffer)) != -1) {   
                     toClient.write(buffer, 0, i);  
                     
                 }   
                 fis.close();   
                 toClient.flush();   
                 toClient.close();   
                 try {
                 response.wait();
           } catch (InterruptedException e) {
            e.printStackTrace();
           }  
             } else {   
                PrintWriter out = response.getWriter();   
                out.print("<script>");   
                out.print("alert(\"not find the file\")");   
                out.print("</script>");   
             }   
         } catch (IOException ex) {   
            PrintWriter out = response.getWriter();   
                out.print("<script>");   
                out.print("alert(\"not find the file\")");   
                out.print("</script>");   
         }