管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-03-18 cb9fbcd27a288d0c61d85fa13ff5fc8eb1f4deab
src/main/java/com/lf/server/service/all/UploadAttachService.java
@@ -4,13 +4,18 @@
import com.lf.server.controller.all.BaseController;
import com.lf.server.entity.all.*;
import com.lf.server.entity.ctrl.KeyValueEntity;
import com.lf.server.entity.data.DownloadEntity;
import com.lf.server.entity.data.MetaEntity;
import com.lf.server.entity.data.MetaFileEntity;
import com.lf.server.entity.sys.AttachEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.*;
import com.lf.server.mapper.all.BasicMapper;
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.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -20,10 +25,7 @@
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
 * 上传附件服务类
@@ -36,6 +38,9 @@
    @Autowired
    AttachService attachService;
    @Autowired
    DownloadMapper downloadMapper;
    private static String tabs = "''";
@@ -408,7 +413,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);
@@ -417,7 +422,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);
@@ -431,6 +436,63 @@
            return null;
        }
        return "";
        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(false);
        addZipFiles(zip, params, list);
        DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, null);
        int rows = downloadMapper.insert(downloadEntity);
        return rows > 0 ? downloadEntity.getGuid() : null;
    }
    /**
     * 添加Zip文件
     */
    private void addZipFiles(ZipFile zip, ZipParameters params, List<AttachEntity> list) {
        int i = 1;
        String uploadPath = pathHelper.getConfig().getUploadPath();
        for (AttachEntity ae : list) {
            try {
                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);
            }
        }
    }
    /**
     * 获取下载实体类
     */
    private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) {
        DownloadEntity de = new DownloadEntity();
        de.setName(FileHelper.getFileName(file));
        // 1-Shp文件,2-专题图,3-元数据,4-业务数据,5-管道分析,6-统计报告,7-附件
        de.setType(7);
        de.setSizes(FileHelper.sizeToMb(new File(file).length()));
        de.setDepid(ue.getDepid());
        de.setDcount(0);
        de.setPwd(pwd);
        de.setUrl(FileHelper.getRelativePath(file));
        de.setDescr("附件文件");
        de.setGuid(FileHelper.getFileMd5(file));
        de.setCreateUser(ue.getId());
        // de.setGeom(null)
        return de;
    }
}