程序员一代码搬运工
【Java学习心得和Java学习技术个人总结】

java批量打包下载springmvc

QQ图片20150922155843

/**
	 * 下载 报告资料
	 * @param request
	 * @param response
	 * @param reportId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="/downReport")
	public String downReport(HttpServletRequest request,HttpServletResponse response,long reportId) throws IOException{
	    
		COM_REPORT_PATH=rb.getString("COM_REPORT_PATH");
		User user= userManagerService.getCurrentUser();
		if(user==null||user.getOid()==0){
			return null;
		}
		Report report = reportoService.getReportByOid(3473408l);
		
		    String tmpFileName = "report.zip";  
	        byte[] buffer = new byte[1024];  
	        String strZipPath = COM_REPORT_PATH+"/"+user.getOid()+"/"+report.getOid()+"/"+tmpFileName;
	        //FilePath + tmpFileName;  
	        try {  
	            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(  
	                    strZipPath));  
	            // 需要同时下载的两个文件result.txt ,source.txt  
	            String []filePath=report.getZipAddress().split(",");
	            File[] file1 =new File[filePath.length] ;
	       
	            for(int i=0;i<filePath.length;i++){
	            	file1[i]=new File(filePath[i]);
	            }
	            
	            
	            
	            for (int i = 0; i < file1.length; i++) {  
	                FileInputStream fis = new FileInputStream(file1[i]);  
	                out.putNextEntry(new ZipEntry(file1[i].getName()));  
	                //设置压缩文件内的字符编码,不然会变成乱码  
	                out.setEncoding("GBK");  
	                int len;  
	                // 读入需要下载的文件的内容,打包到zip文件  
	                while ((len = fis.read(buffer)) > 0) {  
	                    out.write(buffer, 0, len);  
	                }  
	                out.closeEntry();  
	                fis.close();  
	            }  
	            out.close();  
	            this.downloadFile(strZipPath,response);  
	        } catch (Exception e) {  
	         e.printStackTrace();
	        }  
	        return null;  
	}
	
	
	
    public void downloadFile(String fileName,HttpServletResponse response){  
        response.setCharacterEncoding("utf-8");  
        response.setContentType("multipart/form-data");  
  
        response.setHeader("Content-Disposition", "attachment;fileName=report.zip");  
        try {  
            File file=new File(fileName);  
            System.out.println(file.getAbsolutePath());  
            InputStream inputStream=new FileInputStream(file);  
            OutputStream os=response.getOutputStream();  
            byte[] b=new byte[1024];  
            int length;  
            while((length=inputStream.read(b))>0){  
                os.write(b,0,length);  
            }  
            inputStream.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
	

//D:/report/1835009/3473408/report_2.zip,D:/report/1835009/3473408/report_2.zip,D:/report/1835009/3473408/report_3.zip,D:/report/1835009/3473408/report_4.zip,D:/report/1835009/3473408/report_5.zip

赞(0)
未经允许不得转载:程序员一代码搬运工 » java批量打包下载springmvc
分享到: 更多 (0)