吉安感知网项目-后端
xiebin
2026-01-06 d207a86cdf1ab52ef8cb7cd83bad8fceab8038cf
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package org.sxkj.common.utils.file;
 
 
import org.sxkj.common.constant.CommonConstant;
import org.sxkj.common.model.OdmUrlDto;
 
import java.io.*;
import java.nio.file.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.*;
 
public class FileDownloader {
 
    public FileDownloader() {
        // 构造方法
    }
 
    // 按照 jobId 分类下载并压缩文件
    public void downloadAndZipFilesByJobId(Map<String, List<OdmUrlDto>> jobIdToFilePathsMap, String localSaveDir, String zipFileName) throws Exception {
        // 创建目标文件夹路径
        Path localSavePath = Paths.get(localSaveDir);
        if (!Files.exists(localSavePath)) {
            Files.createDirectories(localSavePath);
        }
        String zipFilePath = localSavePath.resolve(zipFileName).toString();
        // 创建压缩文件输出流
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
            zos.setLevel(Deflater.BEST_COMPRESSION);  // 使用最高压缩级别
            // 遍历每个 waylineJobId 对应的文件列表
            for (Map.Entry<String, List<OdmUrlDto>> entry : jobIdToFilePathsMap.entrySet()) {
                // waylineJobId 文件夹名
                String key = entry.getKey();
                // 文件路径列表
                List<OdmUrlDto> odmUrlDtos = entry.getValue();
                // 将每个文件添加到 waylineJobId 文件夹中
                for (OdmUrlDto odmUrlDto : odmUrlDtos) {
//                    String fullFilePath = "E:\\work\\temp\\odm\\odm_orthophoto.tif";
                    String fullFilePath = odmUrlDto.getPath();
                    File file = new File(fullFilePath);
                    if (!file.exists()) {
                        throw new FileNotFoundException("File not found: " + fullFilePath);
                    }
                    // 压缩文件到对应的 waylineJobId 文件夹
                    zipFile(file, key,odmUrlDto.getDkbh(), zos);
                }
            }
        }
    }
 
    /**
     * 将文件压缩到指定的 jobId 文件夹下
     * @param file
     * @param key 文件夹
     * @param dkbh
     * @param zos
     * @throws IOException
     */
    private void zipFile(File file, String key,String dkbh, ZipOutputStream zos) throws IOException {
        // 将文件放入以 jobIdFolder 命名的文件夹中
        String entryName = key + "/" + dkbh + ".tif";
        zos.putNextEntry(new ZipEntry(entryName));
 
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = fis.read(buffer)) >= 0) {
                zos.write(buffer, 0, length);
            }
        }
 
        zos.closeEntry();
    }
 
 
    public static long convertToTimestampString(String dateStr) {
        // 定义日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat(CommonConstant.YYYY_MM_DD_HH_MM_SS);
        try {
            // 将字符串解析为Date对象
            Date date = dateFormat.parse(dateStr);
            // 获取时间戳(毫秒)
            return date.getTime();
        } catch (java.text.ParseException e) {
            throw new RuntimeException(e);
        }
    }
}