zrj
2024-11-05 b8f2c19b869986efa519f1846b63b11378147861
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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<Map<String,Object>> propertyList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Map<String,Object> 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);
        }
    }
}