java常用工具类:实现文件下载
java常用工具类
1.java常用工具类:java实现Excel文件上传下载(模板)-hutool_hutool做excel下载-CSDN博客
2.java常用工具类:java实现excel模板导入数据,允许模板中部分数据字段为空_java导入excel,字段不能为空-CSDN博客
3.使用pinyin4j库实现中文转拼音-CSDN博客
4.java常用工具类:java实现Excel导入和导出_java 导出带参数-CSDN博客
5.java常用工具类:vue+java+aes进行加密解密(ECB、CBC模式)_vue aes加密 java解密-CSDN博客
6.java常用工具类:实现base64加盐编码、解码-CSDN博客
7.java常用工具类:判断对象中属性全是否为空-CSDN博客
8.java常用工具类:实现文件下载-CSDN博客
9.java常用工具类:计算文件大小-CSDN博客
10.java常用工具类:调用第三方接口-CSDN博客
11.java常用工具类:实现MD5加密(加盐)-CSDN博客
12.java常用工具类:生成唯一id-CSDN博客
引言
本文是实现单文件下载,和多文件打成zip压缩包进行下载
pom文件
<!-- hutool--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.1.0</version></dependency>
常量类:Constant
/*** @Author majinzhong* @Date 2024/3/27 14:15* @Version 1.0*/
public class Constant {//windows上传文件路径public static final String WINDOWS_PATH=System.getProperty("user.dir") + "/src/main/resources/deduction/";//linux上传文件路径public static final String LINUX_PATH="/home/upload/deduction/";
}
工具类:DownloadFileUtil
import cn.hutool.core.io.FileUtil;import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;/*** @Author majinzhong* @Date 2024/7/24 15:50* @Version 1.0*/
public class DownloadFileUtil {/*** 单个文件下载* @param response* @param filePath*/public static void oneFileDownload(HttpServletResponse response,String filePath){String path = "";//判断需要上传位置boolean windows = FileUtil.isWindows();if (windows) {path = Constant.WINDOWS_PATH;} else {path = Constant.LINUX_PATH;}filePath=path+filePath;// 获取文件名String fileName = new File(filePath).getName();String encodedFileName = null;try {encodedFileName = URLEncoder.encode(fileName, "UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}try {// 设置响应头信息response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"");response.setContentType("application/octet-stream");// 读取文件并将其写入响应输出流try (InputStream in = new FileInputStream(filePath);OutputStream out = response.getOutputStream()) {byte[] buffer = new byte[4096];int bytesRead;while ((bytesRead = in.read(buffer)) != -1) {out.write(buffer, 0, bytesRead);}}} catch (Exception e) {e.printStackTrace();}}/*** 压缩成zip进行多文件下载* @param response* @param pathList*/public static void moreFileDownload(HttpServletResponse response,List<String> pathList){String path = "";//判断需要上传位置boolean windows = FileUtil.isWindows();if (windows) {path = Constant.WINDOWS_PATH;} else {path = Constant.LINUX_PATH;}response.reset();try {response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode("modelSource.zip", "UTF-8"));} catch (UnsupportedEncodingException e) {e.printStackTrace();}response.setCharacterEncoding("utf-8");response.setContentType("application/octet-stream");try(ZipOutputStream zipOutputStream=new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()))) {for(String pathName:pathList) {File file =new File(path+pathName);String fileName=file.getName();zipOutputStream.putNextEntry(new ZipEntry(fileName));try(BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file))){byte[] bytes = new byte[1024];int i=0;while((i=bis.read(bytes))!=-1) {zipOutputStream.write(bytes,0,i);}zipOutputStream.closeEntry();}catch (Exception e) {e.printStackTrace();}}}catch (Exception e) {e.printStackTrace();}}
}