管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-03-25 eba563608c2d059a8dbbc789dd2c567f2302c088
src/main/java/com/lf/server/service/all/UploadAttachService.java
@@ -14,7 +14,9 @@
import com.lf.server.mapper.data.DownloadMapper;
import com.lf.server.service.sys.AttachService;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.ZipParameters;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -43,7 +45,7 @@
    private static String tabs = "''";
    private final static Map<String, String> ATTACH_TABS = new HashMap<>();
    public final static Map<String, String> ATTACH_TABS = new HashMap<>();
    private final static Log log = LogFactory.getLog(UploadAttachService.class);
@@ -272,22 +274,33 @@
        int rows = 0;
        for (Object obj : list) {
            String name = getAnnexName(obj, field);
            if (StringHelper.isEmpty(name)) {
            String[] names = getNames(getAnnexName(obj, field));
            if (null == names || names.length == 0) {
                continue;
            }
            String file = findAnnexFile(name, folder, files);
            if (null == file) {
                continue;
            }
            for (String name : names) {
                String file = findAnnexFile(name, folder, files);
                if (null == file) {
                    continue;
                }
            BaseEntity be = (BaseEntity) obj;
            rows += insertXlsAnnex(ue, meta, be, file);
            files.remove(file);
                rows += insertXlsAnnex(ue, meta, (BaseEntity) obj, file);
            }
        }
        return rows;
    }
    /**
     * 获取名称
     */
    private String[] getNames(String str) {
        if (StringHelper.isEmpty(str)) {
            return null;
        }
        return str.replace(";", ",").replace(",", ",").split(",");
    }
    /**
@@ -374,7 +387,6 @@
        String md5 = FileHelper.getFileMd5(file);
        AttachEntity old = attachService.selectByTabAndGuid(meta.getTab(), be.getEventid(), md5);
        if (null != old) {
            f.delete();
            return 0;
        }
@@ -384,10 +396,13 @@
        AttachEntity ae = getAttachEntity(ue, meta.getTab(), be.getEventid(), fileName, md5, sizes);
        String targetPath = pathHelper.getConfig().getUploadPath() + File.separator + ae.getPath();
        f.renameTo(new File(targetPath));
        int rows = attachService.insert(ae);
        try {
            FileUtils.copyFile(f, new File(targetPath));
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        return rows > 0 ? 1 : 0;
        return attachService.insert(ae);
    }
    /**
@@ -412,7 +427,7 @@
    /**
     * 请求下载
     */
    public String downloadReqForGuids(UserEntity ue, String[] guids) {
    public String downloadReqForGuids(UserEntity ue, List<String> guids) {
        List<AttachEntity> list = attachService.selectByGuids(guids);
        return downloadReq(ue, list);
@@ -421,7 +436,7 @@
    /**
     * 请求下载
     */
    public String downloadReqForTabGuids(UserEntity ue, String tab, String[] guids) {
    public String downloadReqForTabGuids(UserEntity ue, String tab, List<String> guids) {
        List<AttachEntity> list = attachService.selectByTabGuids(tab, guids);
        return downloadReq(ue, list);
@@ -435,20 +450,12 @@
            return null;
        }
        String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
        String filePath = pathHelper.getTempPath(tempName) + File.separator + tempName;
        File file = new File(filePath);
        if (file.exists() && file.isDirectory()) {
            FileHelper.deleteDir(filePath);
        }
        String zipName = tempName + ".zip";
        String zipName = StringHelper.YMDHMS2_FORMAT.format(new Date()) + ".zip";
        String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName;
        ZipFile zip = Zip4jHelper.createZipFile(zipFile, null);
        ZipParameters params = Zip4jHelper.getZipParams();
        addZipFiles(zip, params, file.listFiles());
        ZipParameters params = Zip4jHelper.getZipParams(false);
        addZipFiles(zip, params, list);
        DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, null);
        int rows = downloadMapper.insert(downloadEntity);
@@ -459,14 +466,23 @@
    /**
     * 添加Zip文件
     */
    private void addZipFiles(ZipFile zip, ZipParameters params, File[] files) {
        if (null == files || files.length == 0) {
            return;
        }
        for (File f : files) {
    private void addZipFiles(ZipFile zip, ZipParameters params, List<AttachEntity> list) {
        int i = 1;
        String uploadPath = pathHelper.getConfig().getUploadPath();
        for (AttachEntity ae : list) {
            try {
                zip.addFile(f, params);
                String filePath = uploadPath + File.separator + ae.getPath();
                File file = new File(filePath);
                if (!file.exists() || file.isDirectory()) {
                    continue;
                }
                zip.addFile(file, params);
                FileHeader header = zip.getFileHeader(file.getName());
                if (null != header) {
                    zip.renameFile(header, i + "_" + ae.getName());
                }
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }