• Java实现文件下载Zip压缩


    Java实现文件下载Zip压缩

    目录

    一、 概述

    二、代码功能实现


    一、 概述

    开发过程中碰到一个需求,需要将服务器上的多个文件打包为zip,并进行下载响应到客户端,写了一个Demo总结分享一下,如果有错误希望大家指正!

    二、代码功能实现

    这里实现只是模式本地文件下载Zip,响应的客户端下载

    实现思路

    1. 创建一个临时文件zip
    2. 构建一个Zip文件输出流
    3. 从服务读取文件流放入Zip文件输出流
    4. 把临时文件Zip写入OutputStream
    5. 关闭资源

    1. controller

    1. /**
    2. * 下载数据包
    3. *
    4. * @param response
    5. * @date: 2021/3/25 15:36
    6. * @return: void
    7. */
    8. @GetMapping("downloadDataZip")
    9. public void downloadDataZip(HttpServletResponse response) {
    10. String title = "报告源数据包.zip";
    11. File filePath = new File(FileUtil.getTemplatePath() + File.separator + title);
    12. List<String> srcFiles = Arrays.asList("20210113001.jpg", "20210113003.jpg", "20210113004.jpg", "瞳孔.txt");
    13. FileUtil.zipFiles(srcFiles, filePath);
    14. String filename = System.currentTimeMillis()+"_"+title;
    15. //设置文件路径
    16. if (filePath.exists()) {
    17. FileInputStream fis = null;
    18. BufferedInputStream bis = null;
    19. try {
    20. response.setContentType("application/octet-stream");
    21. response.setHeader("Content-disposition", "attachment; filename=" + new String(filename.getBytes("utf-8"), "ISO8859-1"));
    22. byte[] buffer = new byte[4096];
    23. fis = new FileInputStream(filePath);
    24. bis = new BufferedInputStream(fis);
    25. OutputStream os = response.getOutputStream();
    26. int i = bis.read(buffer);
    27. while (i != -1) {
    28. os.write(buffer, 0, i);
    29. i = bis.read(buffer);
    30. }
    31. } catch (Exception e) {
    32. e.printStackTrace();
    33. } finally {
    34. if (bis != null) {
    35. try {
    36. bis.close();
    37. // 删除临时文件
    38. filePath.delete();
    39. } catch (IOException e) {
    40. e.printStackTrace();
    41. }
    42. }
    43. if (fis != null) {
    44. try {
    45. fis.close();
    46. } catch (IOException e) {
    47. e.printStackTrace();
    48. }
    49. }
    50. }
    51. }
    52. }

    2. FileUtil

    1. public class FileUtil {
    2. /**
    3. * 多图片压缩zip
    4. *
    5. * @param srcFiles 图片名称
    6. * @param zipFile 文件路径
    7. */
    8. public static void zipFiles(List<String> srcFiles, File zipFile) {
    9. // 判断压缩后的文件存在不,不存在则创建
    10. if (!zipFile.exists()) {
    11. try {
    12. zipFile.createNewFile();
    13. } catch (IOException e) {
    14. e.printStackTrace();
    15. }
    16. }
    17. // 创建 FileOutputStream 对象
    18. FileOutputStream fileOutputStream ;
    19. // 创建 ZipOutputStream
    20. ZipOutputStream zipOutputStream ;
    21. // 创建 FileInputStream 对象
    22. BufferedInputStream bis = null;
    23. FileInputStream inputStream = null;
    24. try {
    25. // 实例化 FileOutputStream 对象
    26. fileOutputStream = new FileOutputStream(zipFile);
    27. // 实例化 ZipOutputStream 对象
    28. zipOutputStream = new ZipOutputStream(fileOutputStream);
    29. // 创建 ZipEntry 对象
    30. ZipEntry zipEntry ;
    31. // 遍历源文件数组
    32. for (String file : srcFiles) {
    33. // 将源文件数组中的当前文件读入 FileInputStream 流中
    34. String fileName = file;
    35. String localFileDir = "D:\\file\\video";
    36. fileName = URLDecoder.decode(fileName, "UTF-8");
    37. //获取文件输入流 localFileDir是服务端存储文件的路径
    38. File files = new File(localFileDir + File.separator + fileName);
    39. inputStream = new FileInputStream(files);
    40. // 文件后缀名称
    41. // 实例化 ZipEntry 对象,源文件数组中的当前文件
    42. zipEntry = new ZipEntry(fileName);
    43. zipOutputStream.putNextEntry(zipEntry);
    44. // 该变量记录每次真正读的字节个数
    45. int len;
    46. // 定义每次读取的字节数组
    47. byte[] buffer = new byte[4096];
    48. while ((len = inputStream.read(buffer)) > 0) {
    49. zipOutputStream.write(buffer, 0, len);
    50. }
    51. }
    52. zipOutputStream.closeEntry();
    53. zipOutputStream.close();
    54. fileOutputStream.close();
    55. } catch (IOException e) {
    56. e.printStackTrace();
    57. } finally {
    58. try {
    59. if (bis != null) {
    60. bis.close();
    61. }
    62. if (inputStream != null) {
    63. inputStream.close();
    64. }
    65. } catch (IOException e) {
    66. e.printStackTrace();
    67. }
    68. }
    69. }
    70. /**
    71. * 获取类路径
    72. * return 绝对路径地址
    73. */
    74. public static String getTemplatePath() {
    75. String realPath = FileUtil.class.getClassLoader().getResource("").getFile();
    76. File file = new File(realPath);
    77. realPath = file.getAbsolutePath();
    78. try {
    79. realPath = java.net.URLDecoder.decode(realPath, "utf-8");
    80. } catch (Exception e) {
    81. e.printStackTrace();
    82. }
    83. return realPath;
    84. }
    85. }

    3. 前端页面

    1. <!DOCTYPE html>
    2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>下载功能</title>
    6. <!-- <script th:src="@{/js/jquery-3.3.1.js}"></script>-->
    7. <script src="../static.js/jquery-3.3.1.js"></script>
    8. </head>
    9. <body>
    10. <div>
    11. <h3>下载文件测试</h3>
    12. <!-- <button type="submit" id="submit">上传</button>-->
    13. <a href="/order/sendUploadVoice" class="tab-down-button">下载1</a>
    14. &nbsp; &nbsp; &nbsp;
    15. <a href="/order/downloadUser" class="tab-down-button">下载2</a>
    16. &nbsp; &nbsp; &nbsp;
    17. <a href="/order/downloadDataZip" class="tab-down-button">报告源数据包</a>
    18. <button type="button" onclick="downloadzip()">按钮下载</button>
    19. <!--预览图片-->
    20. <div class="preview_box"></div>
    21. </div>
    22. <script type="text/javascript">
    23. function downloadzip(){
    24. window.location.href = "/order/downloadDataZip"
    25. };
    26. </script>
    27. </body>
    28. </html>
    https://blog.csdn.net/zouliping123456/article/details/115211866
  • 相关阅读:
    linux安装mongodb数据库./mongod -f mongodb.conf失败或者error
    idea项目提交
    spring5.x日志问题log4j
    IDEA使用apache-tomcat-9.0.16控制台Tomcat日志中文乱码问题
    笔记本网线连服务器内网ip配置
    vue项目启动操作
    win10窗口打开一直处于最小化状态问题
    停止 WinDefend Microsoft Defender Antivirus Service
    关于apt命令和yum命令区别
    20200924-4 代码规范
  • 原文地址:https://www.cnblogs.com/sunny3158/p/16656236.html
Copyright © 2020-2023  润新知