java 导出多个excel_java 压缩导出多个excel

这个Java工具类提供了将多个Excel文件打包成ZIP的功能。它通过ZipOutputStream创建ZIP文件,遍历源文件列表,将每个Excel文件读取并写入ZIP,最后关闭条目。此外,还包括了从BLOB对象中导出文件到本地,以及删除指定目录下所有文件的方法。
摘要由CSDN通过智能技术生成

packagecommon.util;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.URLEncoder;importjava.util.List;importjava.util.zip.ZipEntry;importjava.util.zip.ZipOutputStream;importjavax.servlet.http.HttpServletResponse;importoracle.sql.BLOB;

public classZipUtils {/*** 将多个Excel打包成zip文件

*@paramsrcfile

*@paramzipfile*/

public static void zipFiles(Listsrcfile, File zipfile) {byte[] buf = new byte[1024];try{//Create the ZIP file

ZipOutputStream out = new ZipOutputStream(newFileOutputStream(zipfile));//Compress the files

for (int i = 0; i < srcfile.size(); i++) {

File file=srcfile.get(i);

FileInputStream in= newFileInputStream(file);//Add ZIP entry to output stream.

out.putNextEntry(newZipEntry(file.getName()));//Transfer bytes from the file to the ZIP file

intlen;while ((len = in.read(buf)) > 0) {

out.write(buf,0, len);

}//Complete the entry

out.closeEntry();

in.close();

}//Complete the ZIP file

out.close();

}catch(IOException e) {

e.printStackTrace();

}

}

//filename为单个excel的路径和excel的名称,blob就是获取的blob数据public static intexecute(String filename, BLOB blob)

{int success = 1;try{

File blobFile= newFile(filename);

FileOutputStream outStream= newFileOutputStream(blobFile);

InputStream inStream=blob.getBinaryStream();int length = -1;int size =blob.getBufferSize();byte[] buffer = new byte[size];while ((length = inStream.read(buffer)) != -1)

{

outStream.write(buffer,0, length);

outStream.flush();

}

inStream.close();

outStream.close();

}catch(Exception e)

{

e.printStackTrace();

success= 0;

}finally{returnsuccess;

}

}/*** 删除目录下所有的文件;

*@parampath*/

public static booleandeleteExcelPath(File file){

String[] files= null;if(file != null){

files=file.list();

}if(file.isDirectory()){for(int i =0;i

System.out.println("删除成功!");

}else{

System.out.println("删除失败!");

}

}

}returnfile.delete();

}public static voiddownFile(HttpServletResponse response,String serverPath, String str) {

//下面注释代码虽然少,但是慎用,如果使用,压缩包能下载,但是下载之后临时文件夹会被锁住被jvm占用,不能删除//response.setCharacterEncoding("utf-8");//try {//File file=new File(serverPath,str);//response.setHeader("Content-Disposition",//"attachment; filename="+ StringUtil.encodingFileName(str));//response.setContentType("application/octet-stream; charset=utf-8");//InputStream in1 =new FileInputStream(file.getPath());//IOUtils.copy(in1, response.getOutputStream());//

//}//catch (IOException ex) {//ex.printStackTrace();//}

try{

String path= serverPath +"\\"+str;

File file= newFile(path);if(file.exists()) {

InputStream ins= newFileInputStream(path);

BufferedInputStream bins= new BufferedInputStream(ins);//放到缓冲流里面

OutputStream outs = response.getOutputStream();//获取文件输出IO流

BufferedOutputStream bouts = newBufferedOutputStream(outs);

response.setContentType("application/ostet-stream");//设置response内容的类型

response.setHeader("Content-disposition","attachment;filename="

+ URLEncoder.encode(str, "UTF-8"));//设置头部信息

int bytesRead = 0;byte[] buffer = new byte[8192];//开始向网络传输文件流

while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {

bouts.write(buffer,0, bytesRead);

}

bouts.flush();//这里一定要调用flush()方法

ins.close();

bins.close();

outs.close();

bouts.close();

}else{

response.sendRedirect("../error.jsp");

}

}catch(IOException e) {

e.printStackTrace();

}finally{

File file1=newFile(serverPath);

deleteExcelPath(file1); //删除临时目录

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值