数据库读取文件,并打包为zip包,然后下载(含另存为)

package com.unisure.jbpm.comm;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import com.unisure.comm.JADO;
import com.unisure.comm.JADODB;

public class CommTools {
 //将png图片文件和xml文件从数据库中读取出来
 public static String getFile(String deploymentID,HttpServletRequest request) {
     String filetempPath= request.getRealPath("jbpm/temp/");
     System.out.println(filetempPath);
  String fileNameString="";
  try {
   JADO jd=new JADODB("城保数据库");
   Connection conn=jd.getConn();
   String sql="select blob_value_,Name_ from jbpm4_Lob where deployment_=?";
   PreparedStatement pst=conn.prepareStatement(sql);
   pst.setString(1,deploymentID);
   ResultSet rSet=pst.executeQuery();
   while (rSet.next()) {
   String phonteo=rSet.getString("blob_value_");
   String fileName=rSet.getString("Name_");
   fileName=fileName.substring(fileName.indexOf("/")+1, fileName.length());
   fileNameString+=fileName+"-";
    //图片
    InputStream image=rSet.getBinaryStream("blob_value_");
      String len=image.toString();
          long length=len.length();
          int ch = 0;
          byte[] buffer = new byte[(int)length/7];//定义 buffer为缓冲区每次读取内容的长度
          File file = new File(filetempPath+java.io.File.separator+fileName);//将数据写入文件
     //    if(!file.exists())
     //    {
          file.createNewFile();
     //    }

        FileOutputStream newFile = new FileOutputStream(file);
        boolean go = true;
        while(go)
        {
         while((ch = image.read(buffer))!=-1)
         {
          newFile.write(buffer);
         }
          go=false;
        }
   }
   System.out.println("fileNameString===>"+fileNameString);
   //GetZIP(fileNameString);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 
  return fileNameString;
 }
 //将文件压缩为zip
 public static String GetZIP(String filenames,HttpServletRequest request) {
    String filetempPath= request.getRealPath("jbpm/temp/");
   filenames=filenames.substring(0,filenames.length()-1);
   String[] filename=filenames.split("-");
   String pakageName=filename[0].substring(0,filename[0].indexOf("."));
   System.out.println("------pakageName:------"+pakageName);
   String downloadpath=filetempPath+java.io.File.separator+pakageName+".zip";
  try {
   FileOutputStream f = new FileOutputStream(downloadpath);
   ZipOutputStream out = new ZipOutputStream(new DataOutputStream(f));
   File[] allfiles = {new File(filetempPath+java.io.File.separator+filename[0]),new File(filetempPath+java.io.File.separator+filename[1])};  
   for (int i = 0; i <allfiles.length; i++) {
    DataInputStream in =                    
     new DataInputStream(new FileInputStream(allfiles[i]));                
    out.putNextEntry(new ZipEntry(allfiles[i].getName()));                
    int c;                
    while ((c = in.read()) != -1)                    
     out.write(c);                
    in.close();             }            
   out.close();         
   } catch (Exception e) {
   e.printStackTrace();
   } 
   return downloadpath;
 }
 //文件下载
 public static void processzipdownload(String downloadpath,HttpServletResponse response){
  System.out.println("下载:"+downloadpath);
  SimpleDateFormat sdf=new SimpleDateFormat("yyyymmdd");
  String rqnum=sdf.format(new Date());
  String filename=downloadpath.substring(downloadpath.lastIndexOf("\\"));
  response.setContentType("application/octet-stream");
  response.setContentType("application/OCTET-STREAM;charset=gbk");
  response.setHeader("Content-Disposition", "attachment;filename="+rqnum+filename);
  File file = new File(downloadpath);
  FileInputStream fis;
  BufferedOutputStream out;
  try {
   fis = new FileInputStream(file);
   out = new BufferedOutputStream(response.getOutputStream());
  
  byte[] buffer = new byte[1024];  
  int len;    
  while ((len = fis.read(buffer)) != -1) {    
  out.write(buffer, 0, len);  
  out.flush();    
  } 

  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值