管道基础大数据平台系统开发-【后端】-Server
2
13693261870
2023-03-20 54283d6b0fdd3bc1b444e5a9556ca3a740125b5b
2
已修改2个文件
121 ■■■■ 文件已修改
src/main/java/com/lf/server/helper/FileHelper.java 29 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/service/show/DataLibService.java 92 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/helper/FileHelper.java
@@ -7,9 +7,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
@@ -423,4 +421,29 @@
            list.add(file);
        }
    }
    /**
     * 复制文件
     *
     * @param src  源文件
     * @param dest 目录文件
     */
    public static void copyFile(File src, File dest) throws IOException {
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(src);
            os = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
        } finally {
            os.close();
            is.close();
        }
    }
}
src/main/java/com/lf/server/service/show/DataLibService.java
@@ -1,6 +1,7 @@
package com.lf.server.service.show;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lf.server.entity.all.BaseEntity;
import com.lf.server.entity.data.DownloadEntity;
import com.lf.server.entity.sys.AttachEntity;
import com.lf.server.entity.sys.UserEntity;
@@ -178,23 +179,6 @@
    }
    /**
     * 添加Zip文件
     */
    private void addZipFiles(ZipFile zip, ZipParameters params, File[] files) {
        if (null == files || files.length == 0) {
            return;
        }
        for (File f : files) {
            try {
                zip.addFile(f, params);
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
    }
    /**
     * 获取下载实体类
     */
    private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) {
@@ -250,29 +234,38 @@
        Map<String, List<?>> map = new HashMap<>(1);
        map.put(name, list);
        return zipData(ue, map, pwd);
        return zipData(ue, map, annex, pwd);
    }
    /**
     * 打包数据
     */
    private String zipData(UserEntity ue, Map<String, List<?>> map, String pwd) throws Exception {
    private String zipData(UserEntity ue, Map<String, List<?>> map, List<AttachEntity> annex, String pwd) throws Exception {
        String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
        String tempPath = pathHelper.getTempPath(tempName);
        String filePath = tempPath + File.separator + tempName + ".gdb";
        String gdbPath = tempPath + File.separator + tempName + ".gdb";
        String annexPath = tempPath + File.separator + "annex";
        File file = new File(filePath);
        File file = new File(gdbPath);
        if (file.exists() && file.isDirectory()) {
            FileHelper.deleteDir(filePath);
            FileHelper.deleteDir(gdbPath);
        }
        GdbHelper.createGdb(filePath, map);
        file = new File(annexPath);
        if (!file.exists() || !file.isDirectory()) {
            file.mkdirs();
        }
        GdbHelper.createGdb(gdbPath, map);
        createAnnex(annexPath, annex);
        String zipName = tempName + ".gdb.zip";
        String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName;
        ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd);
        ZipParameters params = Zip4jHelper.getZipParams(true);
        addZipFiles(zip, params, file.listFiles());
        // addZipFiles(zip, params, file.listFiles())
        zip.addFolder(new File(gdbPath), params);
        zip.addFolder(new File(annexPath), params);
        String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd));
        DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd);
@@ -350,14 +343,61 @@
    }
    /**
     * 添加Zip文件
     */
    private void addZipFiles(ZipFile zip, ZipParameters params, File[] files) {
        if (null == files || files.length == 0) {
            return;
        }
        for (File f : files) {
            try {
                zip.addFile(f, params);
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
    }
    /**
     * 获取查询附件条件
     */
    private static String getAnnexFilter(boolean flag, List<?> list) {
    private String getAnnexFilter(boolean flag, List<?> list) {
        if (flag) {
            return null;
        }
        List<Integer> gids = new ArrayList<>();
        for (Object obj : list) {
            BaseEntity be = (BaseEntity) obj;
            gids.add(be.getGid());
        }
        return "";
        return StringHelper.join(gids, ",");
    }
    /**
     * 添加附件文件
     */
    private void createAnnex(String targetPath, List<AttachEntity> list) {
        if (null == list || list.isEmpty()) {
            return;
        }
        int i = 1;
        String uploadPath = pathHelper.getConfig().getUploadPath();
        for (AttachEntity attach : list) {
            try {
                File srcFile = new File(uploadPath + File.separator + attach.getPath());
                if (!srcFile.exists() || srcFile.isDirectory()) {
                    continue;
                }
                File destFile = new File(targetPath + File.separator + i++ + "_" + attach.getName());
                FileHelper.copyFile(srcFile, destFile);
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
    }
}