最近,在項目中遇到了需要將一系列的圖片打包下載的需求,借鑒了網上的一些通用方法,就順便分享出來實現的方法,不太記得借鑒的是哪位兄弟的博客了,總之萬分感謝,進入正題,實現打包下載的基本功能:
1.controller層代碼:
/** * 圖片壓縮打包 */ @RequestMapping(value = "/zipFile") public void compressionFile(HttpServletRequest request, HttpServletResponse response,String busiId) throws Exception{ //業務代碼,根據前台傳來的ID查詢到資源表的圖片list SubMetaData subMetaData = subMetaDataService.findByBusiId(busiId); if (subMetaData != null) { List<SubMetaDataAtt> list = subMetaDataAttService.findByDataId(subMetaData.getDataId()); if (list.size() > 0){ subMetaDataAttService.downloadAllFile(request,response,list); } } }
2.service層通用的文件打包下載
/** * 將多個文件進行壓縮打包,解決文件名下載后的亂碼問題 * */ public void downloadAllFile(HttpServletRequest request, HttpServletResponse response, List<SubMetaDataAtt> list) throws UnsupportedEncodingException{ String downloadName = "附件圖片.zip"; String userAgent = request.getHeader("User-Agent"); // 針對IE或者以IE為內核的瀏覽器: if (userAgent.contains("MSIE") || userAgent.contains("Trident")) { downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8"); } else { // 非IE瀏覽器的處理: downloadName = new String(downloadName.getBytes("UTF-8"), "ISO-8859-1"); } //經過上面的名稱處理即可解決文件名下載后亂碼的問題 response.setContentType("multipart/form-data"); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", downloadName)); //response.setHeader("Content-Disposition", "attachment;fileName=" + downloadName); OutputStream outputStream = null; ZipOutputStream zos = null; try { outputStream = response.getOutputStream(); zos = new ZipOutputStream(outputStream); // 將文件流寫入zip中,此方法在下面貼出 downloadTolocal(zos,list); } catch (IOException e) { logger.error("downloadAllFile-下載全部附件失敗",e); }finally { if(zos != null) { try { zos.close(); } catch (Exception e2) { logger.info("關閉輸入流時出現錯誤",e2); } } if(outputStream != null) { try { outputStream.close(); } catch (Exception e2) { logger.info("關閉輸入流時出現錯誤",e2); } } } }
將文件寫入zip中的方法:
private void downloadTolocal(ZipOutputStream zos, List<SubMetaDataAtt> list) throws IOException { //獲取文件信息//此處為業務代碼,可根據自己的需要替換,我在這里是將資源表list循環出取得路徑以及文件名,然后放進ZipEntry中再執行下載。 for (SubMetaDataAtt subMetaDataAtt : list) { String fileId = subMetaDataAtt.getAttId(); String fileName = subMetaDataAtt.getFileAlias()+subMetaDataAtt.getFileSuffixName(); String path = subMetaDataAtt.getFileAbsolutePath(); InputStream is = null; BufferedInputStream in = null; byte[] buffer = new byte[1024]; int len; //創建zip實體(一個文件對應一個ZipEntry) ZipEntry entry = new ZipEntry(fileName); try { //獲取需要下載的文件流 File file= new File(path); if(file.exists()){ is = new FileInputStream(file); } in = new BufferedInputStream(is); zos.putNextEntry(entry); //文件流循環寫入ZipOutputStream while ((len = in.read(buffer)) != -1 ) { zos.write(buffer, 0, len); } } catch (Exception e) { logger.info("下載全部附件--壓縮文件出錯",e); }finally { if(entry != null) { try { zos.closeEntry(); } catch (Exception e2) { logger.info("下載全部附件--zip實體關閉失敗",e2); } } if(in != null) { try { in.close(); } catch (Exception e2) { logger.info("下載全部附件--文件輸入流關閉失敗",e2); } } if(is != null) { try { is.close(); }catch (Exception e) { logger.info("下載全部附件--輸入緩沖流關閉失敗",e); } } } }
3.前台js的請求方法:
注:文件的下載不要使用AJAX請求的方法,這樣是無法響應請求的,一般會采用Window.open的方法。
window.open(context+"/sub/submetadataatt/zipFile?busiId="+downloadId);//這里的downloadId是我需要傳到后台的變量。
總結:關於上傳,下載的操作,實際上是要對於java的IO十分熟悉,才可以玩的轉,大家一定要把握好基礎才可以在項目中游刃有余,不像我需要去借鑒他人的東西,大家一起努力,加油!
