package org.springblade.modules.yw.controller; import lombok.AllArgsConstructor; import org.springblade.common.constant.FileConstant; import org.springblade.common.utils.ShapeFileUtil; import org.springblade.common.utils.ZipUtil; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @AllArgsConstructor @RequestMapping("/yw/downZip") @RestController public class DownZipController { private final FileConstant fileConstant; /** * 导出指定文件夹下文件(压缩成zip) * @param response * @throws IOException */ @GetMapping("/download-folder-as-zip") public void downloadFolderAsZip(HttpServletResponse response) throws IOException { // 要导出的文件夹路径 String sourceFolderPath = fileConstant.getZipTempSavePath(); // 先生成文件 List> propertyList = new ArrayList<>(); for (int i = 0; i < 3; i++) { Map map = new HashMap<>(); map.put("ID", i); map.put("名称", "test" + i); map.put("描述", "测试shp导出" + i); map.put("geometry", "MULTILINESTRING ((114.0888763800001 22.549298400000055, 114.0897166200001 22.54931240800005, 114.09006708000004 22.549318250000056, 114.09104754000009 22.549328150000065))"); propertyList.add(map); } // 导出shp 文件 ShapeFileUtil.exportShp(propertyList, "test", "MultiLineString",sourceFolderPath); // ZIP文件名 String zipFileName = "exported_folder.zip"; response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + "\""); try ( BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream()); ZipOutputStream zipOut = new ZipOutputStream(bos); ) { ZipUtil.zipFolder(sourceFolderPath, zipOut); } } }