JAVA struts2框架下zip打包文件下载
来源:互联网 发布:手机数字键盘钢琴软件 编辑:程序博客网 时间:2024/05/13 19:05
用ant.jar中apache.tools中的ZipOutputStream来作为出入流(可能会有其他外层修饰),以byteArrayInputStream来作为输入流。
业务代码:
注意点:
上代码:
action代码:
//代理下载public String agentDownload(){int dataSourceId = LoginInfo.getLoginUser().getDataSourceId();adminService = new AdminBLL(dataSourceId);String root = ServletActionContext.getServletContext().getRealPath("\\");String srcPath = root + "softs"+ File.separator+"Chart.js-master.zip";File file = new File(srcPath);input = adminService.downloadAgent(file);setAgentName("agent1.2");setMf("adm_monitor");return "agentDownload";}
业务代码:
public InputStream downloadAgent(File file){InputStream input = null;ByteArrayOutputStream out = new ByteArrayOutputStream();CheckedOutputStream cos = new CheckedOutputStream(out, new CRC32());ZipOutputStream zip = new ZipOutputStream(cos);ZipEntry entry = new ZipEntry(file.getName());InputStream is = null;try {zip.putNextEntry(entry);is = new BufferedInputStream(new FileInputStream(file));int buffer = 2048;byte[] data = new byte[buffer];int k;while( (k = is.read(data, 0, buffer)) != -1){zip.write(data, 0, k);}zip.flush();zip.closeEntry();} catch (IOException e) {e.printStackTrace();}finally{try {if(zip != null){zip.close();//必须要关闭,不然会出错input = new ByteArrayInputStream(out.toByteArray());}if(is != null){is.close();}} catch (IOException e){e.printStackTrace();}}return input;}
注意点:
1、zip.flush()\zip.closeEntry()\zip.close();方法一定要调用,在调用close()方法后,才能开始转入输入流。
0 0
- JAVA struts2框架下zip打包文件下载
- 下载文件打包 zip java
- Struts2多文件zip打包下载
- java 打包zip文件
- java 打包Zip文件
- JAVA文件打包ZIP
- php zip打包下载文件
- zip压缩打包文件下载
- spingmvc 文件下载打包zip
- PHP打包下载zip文件
- 打包zip文件并下载
- 【Java】Java实现批量打包文件Zip下载
- java 将多个文件打包成一个zip后进行下载
- JAVA 根据Url把多文件打包成ZIP下载
- java实现文件自动打包成zip并下载…
- JAVA ZIP压缩打包下载
- linux zip,linux下打包zip文件
- linux zip,linux下打包zip文件
- leetcode笔记:Number of 1 Bits
- JS私有变量和静态私有变量
- leetcode——219—— Contains Duplicate II
- 查找轮廓总结
- ofstream和ifstream详细用法
- JAVA struts2框架下zip打包文件下载
- 【Android Framework】 Android Binder 机制解析
- Ubuntu使用caore dump定位程序错误
- Java maven 环境下使用proguard-maven-plugin 插件混淆代码
- 【JQuery】:visible过滤选择器
- JPA
- iOS 上架app的过程当中出现the Info.plist is missing or could not be parsed.经历
- 安卓快速入门系列1(通过插件使用java8的语法)
- Android性能优化之渲染优化的8个点