Java将多个文件打包成ZIP并下载
需求是多个文件需要同时打包成zip压缩文件并下载到本地,首先我需要的是知道下载文件的路径。我有一个专门的sys_file_info表,表中有对应的文件路径。业务表中的文件地址放的就是文件表的id值。下面是代码:
1.Mapper层
SELECT i.file_path filePath, i.file_name fileName FROM ves_data_poc p
LEFT JOIN sys_file_info i ON p.script_url = i.file_id
WHERe i.file_path IS NOT NULL
AND p.id IN
#{id}
List
2.Service层
idStr 是前端传来的多个id值用逗号连接的字符串
void download(String idStr, HttpServletResponse response);
@Override
public void download(String idStr, HttpServletResponse response) {
if (StringUtils.isNotEmpty(idStr)) {
String[] ids = idStr.split(",");
List
3.Controller层
@Log(title = "数据管理-POC", businessType = BusinessType.EXPORT)
@GetMapping("/download")
public void download(String ids, HttpServletResponse response) {
vesDataPocService.download(ids, response);
}
4.工具类
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class CompressUtil {
public static void compress(List