java 文件压缩和下载
来源:互联网 发布:aerial 屏保 windows 编辑:程序博客网 时间:2024/05/16 12:10
压缩代码:
public class FileZipUtil { /** * @description:将文件/文件夹生成指定格式的压缩文件 * @autor:ym10159 * @date:2017年12月22日 上午9:53:13 * @param srcPath 源文件路径 * @param targetPath 压缩文件保存路径 */ public static void compressedFile(String srcPath, String targetPath, String zipFileName){ File srcFile = new File(srcPath); File targetFile = new File(targetPath); //压缩目录不存在,则创建 if (targetFile.isDirectory() && !targetFile.exists()) { targetFile.mkdirs(); } FileOutputStream fos = null; ZipOutputStream zos = null;try {fos = new FileOutputStream(targetPath + File.separator + zipFileName);zos = new ZipOutputStream(new BufferedOutputStream(fos)); compress(zos, srcFile, ""); } catch (Exception e) {e.printStackTrace();} finally {try {if(null != zos){zos.close();}if(null != fos){fos.close();}} catch (IOException e) {e.printStackTrace();}} } private static void compress(ZipOutputStream zos, File file, String baseDir) throws Exception { if (file.isDirectory()) { baseDir = file.getName() + File.separator; compressDir(zos, file, baseDir); } else if (file.isFile()){ compressFile(zos, file, baseDir); } } private static void compressDir(ZipOutputStream zos, File file, String baseDir) throws Exception{ File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { /**递归调用*/ compress(zos, files[i], baseDir); } } private static void compressFile(ZipOutputStream zos, File file, String baseDir) throws Exception{ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); zos.putNextEntry(new ZipEntry(baseDir + file.getName())); int len = 0; byte[] buffer = new byte[1024]; while ((len = bis.read(buffer)) != -1) { zos.write(buffer, 0, len); } bis.close(); }}
注意点:
1.流的关闭顺序,否则会报stream closed异常
2.递归的调用
下载代码:
public void download(HttpServletRequest request, HttpServletResponse response) throws IOException{String zipDownloadPath = attachmentService.download(orderCodeList);File file = new File(zipDownloadPath);InputStream fis = new BufferedInputStream(new FileInputStream(file));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();response.reset();response.setCharacterEncoding("UTF-8");String filename = file.getName().replaceAll("\\s", "");filename = URLEncoder.encode(filename,"UTF-8");response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename));response.addHeader("Content-Length", "" + file.length());OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");toClient.write(buffer);toClient.flush();toClient.close();}
注意点:
1.下载弹出框的设置,
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename));
2.在谷歌和火狐浏览器中,不能更改目录存放,火狐默认存放在桌面,谷歌默认存放在C:\Users\用户名\Downloads下。而QQ浏览器有更改目录的功能。至于在谷歌和火狐浏览器中,不能更改存放目录的原因暂时还未解决。有解决的人,记得告诉我答案。阅读全文
0 0
- java 文件压缩和下载
- java多文件压缩下载
- java-flex 批量下载文件-压缩下载
- java实现文件压缩下载----压缩下载zip
- java创建一个文件压缩并下载
- 用java实现文件下载并压缩
- java下载压缩包文件zip
- java压缩和解压文件
- 【Java】多文件压缩下载(调用浏览器下载)
- Django下载大文件和压缩zip文件
- C# 文件压缩下载
- zip压缩、文件下载
- FileToZip文件压缩下载
- C# 文件压缩下载
- 文件压缩\修改\下载
- 压缩下载文件
- 【Java】Java实现zip压缩多个文件下载
- java 下载Excel动态模板文件压缩包
- 图片校验是否存在或破损
- JNI 接口 简介 与 示例 java 调用 c++
- Qt 的学习网站
- Spring+Mybatis+Mysql搭建分布式数据库访问框架
- MATLAB中的常用函数
- java 文件压缩和下载
- 利用百度地图实现支付宝“到位”功能(地图模式)
- 深入理解java虚拟机
- IOS使用mailcore2发送邮件,qq邮箱发送
- 游侠原创:在等级保护中被广泛应用的安全审计技术(2017年版)
- Android-----------广告图片轮播控件
- 2017.12.22
- AttributeError: module 'tensorflow' has no attribute 'select'
- JS工具类