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();
}
}
}