尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

设备二维码图片下载

设备二维码图片下载
📅 发布时间:2026/6/19 21:44:00

package com.inovance.paas.tpm.infra.util;

import org.apache.commons.compress.utils.IOUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class DownloadZipUtil {
public static void downloadZip(HttpServletResponse response, String sourceFolderPath, String zipName, String filePath){
// 获取文件分隔符(动态适配操作系统)
String separator = File.separator;
String zipFilePath = filePath + "zip"+ separator + zipName +".zip"; // 生成的ZIP文件路径

    FileOutputStream fos;ZipOutputStream zos;try {fos = new FileOutputStream(zipFilePath);zos = new ZipOutputStream(fos);addFolderToZip("", sourceFolderPath, zos);zos.close();fos.close();File downloadFile = new File(zipFilePath);FileInputStream inputStream = new FileInputStream(downloadFile);response.setContentType("application/zip");response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadFile.getName() + "\"");IOUtils.copy(inputStream, response.getOutputStream());  // 使用IOUtils将ZIP响应给客户端inputStream.close();response.flushBuffer();doGet(response, zipFilePath, zipName);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
}private static void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {File folder = new File(srcFile);if (folder.isDirectory()) {addFolderToZip(path, srcFile, zip);} else {byte[] buf = new byte[1024];int len;FileInputStream in = new FileInputStream(srcFile);zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));while ((len = in.read(buf)) > 0) {zip.write(buf, 0, len);}}
}private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws IOException {File folder = new File(srcFolder);for (String fileName : folder.list()) {if (path.equals("")) {addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);} else {addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);}}
}protected static void doGet(HttpServletResponse response, String filePath, String zipName) throws IOException {//设置文件路径File downloadFile = new File(filePath);FileInputStream inStream = new FileInputStream(downloadFile);//设置下载的文件类型为zipresponse.setContentType("application/zip");//设置文件名,此处以zipFile.zip为例response.setHeader("Content-Disposition", "attachment; filename="+zipName+".zip");//获取输出流ServletOutputStream outputStream = response.getOutputStream();//创建缓冲区byte[] buffer = new byte[1024];int bytesRead = -1;//将文件写入输出流while ((bytesRead = inStream.read(buffer)) != -1) {outputStream.write(buffer, 0, bytesRead);}inStream.close();outputStream.flush();outputStream.close();
}

}

相关新闻

  • neural network中的tensor是什么?
  • 2025年工厂维保,工厂机电维修,工厂应急维修,工厂运维服务厂家推荐排行榜,专业高效与全方位保障之选!
  • 2025/10/17

最新新闻

  • 巴特沃斯滤波器实战:Python信号处理从原理到可视化
  • Draggabilly终极指南:三大核心配置让你的拖拽交互更智能
  • 2026洛阳防水补漏维修团队实测盘点TOP4:洛阳业主房屋渗漏修缮靠谱选择 - 宅安选房屋修缮
  • 深耕禅城防水领域 匠心守护安居|微顺虹防水:初心筑品质,服务护万家 - 徽顺虹
  • 国产AI生图开源困境:技术能力与生态节奏的错位
  • 曦云C系列GPU如何实现GLM-5.1 Day 0全栈适配

日新闻

  • 5分钟掌握Python进化算法:Geatpy高性能优化工具完全指南
  • Microchip 24AA044 EEPROM选型与应用全指南:从参数解析到实战编程
  • 华为的鸿蒙到底有多牛?为什么称作遥遥领先?

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号