Java把文件夹打成压缩包并导出
来源:互联网 发布:seo快速排名 编辑:程序博客网 时间:2024/06/05 15:45
1.前台调用后台方法export_zip.htm:
@Controllerpublic class AdminController { private String filePath = AdminController.class.getResource("/").getPath().split("WEB-INF")[0]+ "upload/"; @RequestMapping(value = "export_zip.htm", method = {RequestMethod.GET, RequestMethod.POST }) public void zipwordDownAction(HttpServletRequest request,HttpServletResponse response) throws Exception { //打包文件的存放路径 ZipCompressorByAnt zc = new ZipCompressorByAnt(filePath+ "/file.zip"); //需要打包的文件路径 zc.compress(filePath+ "/file/"); String contentType = "application/octet-stream"; try { //导出压缩包 download(request, response, "upload/file.zip", contentType,encodeChineseDownloadFileName(request, "file.zip")); } catch (Exception e) { request.getSession().setAttribute("msg", "暂无内容"); } //如果原压缩包存在,则删除 File file=new File(filePath+ "/file.zip"); if(file.exists()){ file.delete(); } } /** * 下载文件 */ public static void download(HttpServletRequest request,HttpServletResponse response, String storeName, String contentType,String realName) throws Exception { response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); BufferedInputStream bis = null; BufferedOutputStream bos = null; String ctxPath =FileUtil.class.getResource("/").getPath().split("WEB-INF")[0]; String downLoadPath = ctxPath + storeName; long fileLength = new File(downLoadPath).length(); response.setContentType(contentType); response.setHeader("Content-disposition", "attachment; filename=" + new String(realName.getBytes("utf-8"), "ISO8859-1")); response.setHeader("Content-Length", String.valueOf(fileLength)); bis = new BufferedInputStream(new FileInputStream(downLoadPath)); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } bis.close(); bos.close(); } /** * 对文件流输出下载的中文文件名进行编码 屏蔽各种浏览器版本的差异性 */ public static String encodeChineseDownloadFileName(HttpServletRequest request, String pFileName) throws UnsupportedEncodingException { String filename = null; String agent = request.getHeader("USER-AGENT"); if (null != agent){ if (-1 != agent.indexOf("Firefox")) {//Firefox filename = "=?UTF-8?B?" + (new String(org.apache.commons.codec.binary.Base64.encodeBase64(pFileName.getBytes("UTF-8"))))+ "?="; }else if (-1 != agent.indexOf("Chrome")) {//Chrome filename = new String(pFileName.getBytes(), "ISO8859-1"); } else {//IE7+ filename = java.net.URLEncoder.encode(pFileName, "UTF-8"); filename = StringUtils.replace(filename, "+", "%20");//替换空格 } } else { filename = pFileName; } return filename; }
2.调用工具类:
import java.io.File;import org.apache.tools.ant.Project;import org.apache.tools.ant.taskdefs.Zip;import org.apache.tools.ant.types.FileSet;public class ZipCompressorByAnt { private File zipFile; public ZipCompressorByAnt(String pathName) { zipFile = new File(pathName); } public void compress(String srcPathName) { File srcdir = new File(srcPathName); if (!srcdir.exists()) throw new RuntimeException(srcPathName + "不存在!"); Project prj = new Project(); Zip zip = new Zip(); zip.setProject(prj); zip.setDestFile(zipFile); FileSet fileSet = new FileSet(); fileSet.setProject(prj); fileSet.setDir(srcdir); //fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夹 eg:zip.setIncludes("*.java"); //fileSet.setExcludes(...); 排除哪些文件或文件夹 zip.addFileset(fileSet); zip.execute(); } }
阅读全文
0 0
- Java把文件夹打成压缩包并导出
- java导出多个excel并打成zip包
- Java把同一个文件夹下的不同文件夹打成一个.zip格式的包
- Java 让excel导出大量数据进行分表打成压缩包进行下载
- java 将文件打成zip压缩包
- java 将多个文件打成压缩包
- JAVA 将文件夹打成ZIP包
- java将文件夹打成war包
- JAVA压缩图片并打成ZIP
- JAVA压缩图片并打成ZIP
- 把java项目打成war包
- 把Java程序打成Jar包
- 把一个java程序打成jar包
- 如何把java程序打成JAR包并能自动运行
- 如何把Java程序打成JAR包并能自动运行
- java把文件夹(目录)子文件夹打成zip文件
- java项目打成jar包,并执行
- java 导出压缩包
- MyBatis mapper.xml中SQL处理小于号与大于号
- c语言 m位整数中的n个最大素数
- POI使用案例01---读取excel的数据
- Kalman Filter Study (include Monte Carlo Position)
- 项目管理 第一章项目选择
- Java把文件夹打成压缩包并导出
- Java反射应用实例1
- 微信小程序实现tabs选项卡效果简单代码实例
- js调用ios原生多种写法参考及js调用android原生方法参考
- Java垃圾回收机制浅析
- 利用windows.h头文件遍历目录
- 进程间通信(IPC)介绍
- Java虚拟机架构
- WIFI-扫描随笔