天天看點

java項目(ssm架構)實作批量下載下傳圖檔并打包壓縮為zip檔案

jsp

<a class="btn btn-primary radius" href="worker/zipfileDownload_workerCard" target="_blank" rel="external nofollow" ><i class="Hui-iconfont">&#xe600;</i> 證件下載下傳</a></span>
           

Controller.java 

/**
		 *檔案壓縮下載下傳
		 *billname:檔案名
		 *filename:檔案存放路徑
		 */
		 @RequestMapping("/zipfileDownload_workerCard")
		 public void zipfileDownload_workerCard(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException{
		        //響應頭的設定
		        response.reset();
		        response.setCharacterEncoding("utf-8");
		        response.setContentType("multipart/form-data");
		       
		        //設定壓縮包的名字
		         //解決不同浏覽器壓縮包名字含有中文時亂碼的問題
		        HttpSession session = request.getSession();
		        
		        String billname ="workerCard-"+session.getAttribute("projectWorktypeId");
		        String downloadName = billname+".zip";
		        //傳回用戶端浏覽器的版本号、類型
		        String agent = request.getHeader("USER-AGENT");  
		        try {
		        	//針對IE或者以IE為核心的浏覽器:  
		            if (agent.contains("MSIE")||agent.contains("Trident")) {
		                downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");
		            } else {
		            	//非IE浏覽器的處理:
		                downloadName = new String(downloadName.getBytes("UTF-8"),"ISO-8859-1");
		            }
		        } catch (Exception e) {
		            e.printStackTrace();
		        }
		        response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");
 
		        //設定壓縮流:直接寫入response,實作邊壓縮邊下載下傳
		        ZipOutputStream zipos = null;
		        try {
		            zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
		            zipos.setMethod(ZipOutputStream.DEFLATED); //設定壓縮方法 
		        } catch (Exception e) {
		            e.printStackTrace();
		        }
 
		        //循環将檔案寫入壓縮流
		        DataOutputStream os = null;
		        Worker worker = new Worker();
		        worker.setProjectWorktypeId((Integer)session.getAttribute("projectWorktypeId"));
            //從資料庫中取出要下載下傳的圖檔路徑、并循環寫入壓縮
		        List<Worker> workerList = workerService.findAllWorkerByWhere(worker);
		        for (Worker worker2 : workerList) {
		        	String filename =worker2.getWorkerScanFront();
		        	String removeStr = "/upload/workerCard";
		        	filename = filename.replace(removeStr, "");//去掉相對路徑中的兩個目錄路徑
		        		String modipath = request.getSession().getServletContext().getRealPath("/upload/workerCard"+filename);
		        		File file = new File(modipath);
		        		if(file.exists()){
		        			try {
		        				//添加ZipEntry,并ZipEntry中寫入檔案流
		        				//這裡,加上i是防止要下載下傳的檔案有重名的導緻下載下傳失敗
		        				zipos.putNextEntry(new ZipEntry(filename));
		        				os = new DataOutputStream(zipos);
		        				InputStream is = new FileInputStream(file);
		        				byte[] b = new byte[100];
		        				int length = 0;
		        				while((length = is.read(b))!= -1){
		        					os.write(b, 0, length);
		        				}
		        				is.close();
		        				zipos.closeEntry();
		        			} catch (IOException e) {
		        				e.printStackTrace();
		        			} 
		        		}
		    	}
			     //關閉流
		        try {
		            os.flush();
		            os.close();
		            zipos.close();
		        } catch (IOException e) {
		            e.printStackTrace();
		        }    
 
		    }
           

版權聲明:本文為CSDN部落客「weixin_34318956」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。

原文連結:https://blog.csdn.net/weixin_34318956/article/details/91498933