java下载压缩包简化代码
来源:互联网 发布:天津加工中心编程招聘 编辑:程序博客网 时间:2024/06/04 19:40
/** * 批量打包下载文件生成zip文件下载 * */@CrossOrigin@RequestMapping(value = "/xiazai", method = RequestMethod.GET)public ResponseDto downloadFiles(HttpServletRequest request, HttpServletResponse response, String x)throws ServletException, IOException {try {List<String> filePaths = xManager.getRealPaths(x);List<File> files = FileUtil.getFiles(filePaths);if (files == null || files.isEmpty()) {return new ResponseDto(HttpStatus.INTERNAL_SERVER_ERROR, "无可下载文件");}CustomFileUtil.downLoadZipFile(files, response);return null;} catch (Exception e) {logger.error("downloadFiles error :", e.getMessage(), e);return new ResponseDto(HttpStatus.INTERNAL_SERVER_ERROR, "下载失败");}}
package com.x;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.UUID;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;import javax.servlet.http.HttpServletResponse;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class CustomFileUtil {private static Logger logger = LoggerFactory.getLogger(CustomFileUtil.class);public static List<File> getFiles(List<String> filePaths) {List<File> files = new ArrayList<File>();if (filePaths == null || filePaths.isEmpty()) {return null;}for (String t : filePaths) {if (t == null || ("").equals(t)) {continue;}File temp = new File(t);if (temp.exists()) {files.add(temp);}}if (files == null || files.isEmpty()) {return null;}return files;}/** * 将文件写入到zip文件中 * * @param inputFile * @param outputstream * @throws Exception */public static void zipFile(File inputFile, ZipOutputStream outputstream) {FileInputStream inStream = null;BufferedInputStream bInStream = null;try {if (inputFile.isFile()) {inStream = new FileInputStream(inputFile);bInStream = new BufferedInputStream(inStream);ZipEntry entry = new ZipEntry(inputFile.getName());outputstream.putNextEntry(entry);byte[] buffer = new byte[2 * 1024 * 1024];int byteRead = 0;while ((byteRead = bInStream.read(buffer)) != -1) {outputstream.write(buffer, 0, byteRead);}outputstream.closeEntry(); // Closes the current ZIP entry}} catch (IOException e) {logger.error("zipFile IOException error:", e.getMessage(), e);} finally {try {if (bInStream != null) {bInStream.close();// 关闭}if (inStream != null) {inStream.close();}} catch (IOException e) {}}}/** * 打包压缩下载文件 */public static void downLoadZipFile(List<File> files, HttpServletResponse response) throws IOException {String zipName = UUID.randomUUID().toString() + ".zip"; // 在服务器端创建打包下载的临时文件response.setContentType("APPLICATION/OCTET-STREAM");response.setHeader("Content-Disposition", "attachment; filename=" + zipName);ZipOutputStream out = new ZipOutputStream(response.getOutputStream());try {for (int i = 0; i < files.size(); i++) {File file = (File) files.get(i);if (file.exists()) {zipFile(file, out);response.flushBuffer();}}} catch (Exception e) {logger.error("downLoadZipFile error: ", e.getMessage(), e);} finally {if (out != null) {out.close();}}}}
0 0
- java下载压缩包简化代码
- 下载linux kernel 代码压缩包
- Java下载网络压缩包及解压
- java下载压缩包文件zip
- java spring mvc 下载压缩包简洁代码,读取时候不分段读取。
- java下载URL到指定路径(支持压缩包)(代码示例)
- 一段zip压缩包java代码
- java操作压缩包的完整代码
- 关于压缩包上传和下载示例代码
- C#压缩包下载
- Java 简化代码
- Lombok简化Java代码
- JAVA-代码简化工具
- java 下载Excel动态模板文件压缩包
- Java多文件打包为一个压缩包下载
- 将文件包压缩下载
- 下载excel、压缩包等
- 简化Java代码的技巧
- 编译原理:怎样求FIRST集与FOLLOW集 PS:整合转载大全
- centos 6.5安装MySQL
- 补题清单
- 虚拟机里面的centos不能上网,ping不通网关或者外网地址b
- java学习 基础变量
- java下载压缩包简化代码
- 【ARM-LInux开发】如何运行wayland
- UILabel显示固定行数的问题
- Mysql+mybatis 反转生成实体类和Mapper文件以及Mapper.xml文件
- java 中整型与字符型的相互转换
- java interrupt()方法
- linux search.
- linux基础命令
- week3_day1_SQL SERVER