打包并下载文件
2015-09-07 16:06
197 查看
CdncontractbillDAO billDAO = new CdncontractbillDAO();
List billlist = billDAO.findAll();
//要下载的zip文件,名为DownLoad.zip
File zipFile = new File(System.getProperty("cdncharge_pdf")+"DownLoad.zip");
try {
//用这个构造最终压缩包的输出流
ZipOutputStream zipoutStream = new ZipOutputStream(new FileOutputStream(zipFile));
InputStream is;
byte[] bufferArea = new byte[1024];//读写缓冲区
//循环所有文件,将文件打包
for(Object bi : billlist){
Cdncontractbill bill = (Cdncontractbill)bi;
File file = new File(bill.getPath());
if(file.exists()){
FileInputStream zipinSource = new FileInputStream(file);
int read = 0;
ZipEntry zipEntry = new ZipEntry(file.getName());
zipoutStream.putNextEntry(zipEntry);//定位到该压缩条目位置,开始写入文件到压缩包中
while((read = zipinSource.read(bufferArea, 0, 1024)) != -1)
{
zipoutStream.write(bufferArea, 0, read);
}
zipoutStream.closeEntry();
zipinSource.close();
}
}
zipoutStream.close();
is = new FileInputStream(zipFile);
Filedownload.save(is, "", zipFile.getName());
} catch (Exception e) {
e.printStackTrace();
}
//删除生成的打包文件
zipFile.delete();
List billlist = billDAO.findAll();
//要下载的zip文件,名为DownLoad.zip
File zipFile = new File(System.getProperty("cdncharge_pdf")+"DownLoad.zip");
try {
//用这个构造最终压缩包的输出流
ZipOutputStream zipoutStream = new ZipOutputStream(new FileOutputStream(zipFile));
InputStream is;
byte[] bufferArea = new byte[1024];//读写缓冲区
//循环所有文件,将文件打包
for(Object bi : billlist){
Cdncontractbill bill = (Cdncontractbill)bi;
File file = new File(bill.getPath());
if(file.exists()){
FileInputStream zipinSource = new FileInputStream(file);
int read = 0;
ZipEntry zipEntry = new ZipEntry(file.getName());
zipoutStream.putNextEntry(zipEntry);//定位到该压缩条目位置,开始写入文件到压缩包中
while((read = zipinSource.read(bufferArea, 0, 1024)) != -1)
{
zipoutStream.write(bufferArea, 0, read);
}
zipoutStream.closeEntry();
zipinSource.close();
}
}
zipoutStream.close();
is = new FileInputStream(zipFile);
Filedownload.save(is, "", zipFile.getName());
} catch (Exception e) {
e.printStackTrace();
}
//删除生成的打包文件
zipFile.delete();
相关文章推荐
- Java的运行原理
- MongoDB 基础知识
- [置顶] 微信开放平台的第三方平台、全网发布流程、组件API、返回普通文本消息
- OpenGL教程翻译 第十二课 透视投影
- 利用Git和TortoiseGit把代码传输到网络服务器
- ERP知识点(3)采购与库存
- [转] 主流JS框架中DOMReady事件的实现
- TestLink 汉化
- Java生成pdf
- MVC Html.AntiForgeryToken() 防止CSRF攻击
- Android studio dabao
- JAVA高并发学习笔记(一)
- 高并发量网站解决方案
- 在MATLAB中调用封装好的C++函数的方法
- Http通用短信接口开发经验及具体开发实现
- 安卓开发习惯///个人总结加网传经验
- iOS开发 - 证书失效
- spark standalone模式 zeppelin安装
- 算法系列--打印素数(详解)
- C# Regex.IsMatch (正则表达式验证:数字、小数点、邮件、计算表达式)