String path = null ;
InputStream in = null;
String linShiPath = FrameworkContext.getAppPath()+"\\"+"zip"; //打包生成zip
File linShiFile = new File(linShiPath);
if( !linShiFile.exists()){
linShiFile.mkdir();
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String zipName = format.format(new Date()); //包名
File outZipFile = new File(linShiPath+"\\"+zipName+".zip"); //生成的zip文件
try {
ZipOutputStream zipOut = new ZipOutputStream(outZipFile);
InputStream input ; //将文件写出web
for(int i = 0 ; i < arrIds.length ; i++){ //循环文件id得到所有的文件
id = arrIds[i];
//得到该id下的所有文件
List<UploadFile> fileList = uploadFile.listFilesByPid("ES_T_RULEFILES", id); //下载的文件信息
for(int j=0;j<fileList.size();j++){ //循环文件list 得到单个文件累加到zip中
UploadFile upload = fileList.get(j) ;
path = FrameworkContext.getAppPath()+upload.getPath();
File downFile = new File(path); //要下载的文件
in = new FileInputStream(downFile);
zipOut.putNextEntry(new ZipEntry(upload.getName()));
//写入zip文件
int len = 0;
byte b [] = new byte[1024] ;
while((len = in.read(b)) > -1){
zipOut.write(b , 0, len);
}
in.close();
zipOut.closeEntry();
}
}
zipOut.close();
//将文件写出
if(outZipFile.exists()){
input = new FileInputStream(outZipFile) ;
String exp = outZipFile.getName().substring(outZipFile.getName().lastIndexOf("."));
try {
postFile(request, response, input, outZipFile.getName(), exp, "UTF-8");
} catch (Exception e) {
PrintWriter out = null;
response.setContentType("text/html;charset="
+ System.getProperties().get("file.encoding"));
out = new PrintWriter(response.getOutputStream());
out.write("服务器上不存在该文件,可能已被删除!");
out.close();
e.printStackTrace();
}finally{
input.close();
}
}else{
PrintWriter out = null;
response.setContentType("text/html;charset="
+ System.getProperties().get("file.encoding"));
out = new PrintWriter(response.getOutputStream());
out.write("服务器上不存在该文件,可能已被删除!");
out.close();
}
} catch (Exception e) {
PrintWriter out = null;
response.setContentType("text/html;charset="
+ System.getProperties().get("file.encoding"));
out = new PrintWriter(response.getOutputStream());
out.write("服务器上不存在该文件,可能已被删除!");
out.close();
e.printStackTrace();
}finally{
outZipFile.delete();
linShiFile.delete();
}