From cb9fbcd27a288d0c61d85fa13ff5fc8eb1f4deab Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期六, 18 三月 2023 16:44:56 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/service/all/UploadAttachService.java | 131 +++++++++++++++++++++++++++++++++++++------ 1 files changed, 113 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/lf/server/service/all/UploadAttachService.java b/src/main/java/com/lf/server/service/all/UploadAttachService.java index 7d6cc80..84de858 100644 --- a/src/main/java/com/lf/server/service/all/UploadAttachService.java +++ b/src/main/java/com/lf/server/service/all/UploadAttachService.java @@ -1,17 +1,21 @@ package com.lf.server.service.all; -import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 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; @@ -21,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.*; /** * 涓婁紶闄勪欢鏈嶅姟绫� @@ -38,13 +39,14 @@ @Autowired AttachService attachService; + @Autowired + DownloadMapper downloadMapper; + private static String tabs = "''"; private final static Map<String, String> ATTACH_TABS = new HashMap<>(); private final static Log log = LogFactory.getLog(UploadAttachService.class); - - private static final String NO_FILE = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.NOT_FOUND, "鏂囦欢鎵句笉鍒�")); /** * 鑾峰彇琛ㄥ悕 @@ -139,27 +141,34 @@ * 涓嬭浇鏂囦欢 */ public void download(String guid, HttpServletResponse res) { + download(guid, false, res); + } + + /** + * 涓嬭浇鏂囦欢 + */ + public void download(String guid, boolean inline, HttpServletResponse res) { try { if (StringHelper.isEmpty(guid)) { - WebHelper.write2Page(res, NO_FILE); + WebHelper.writeStr2Page(res, StaticData.NO_FILE); return; } AttachEntity entity = attachService.selectByGuid(guid); if (entity == null) { - WebHelper.write2Page(res, NO_FILE); + WebHelper.writeStr2Page(res, StaticData.NO_FILE); return; } - String file = pathHelper.getConfig().getUploadPath() + File.separator + entity.getPath(); - WebHelper.download(file, entity.getName(), res); - } catch (Exception ex) { - try { - String msg = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.ERROR, "鏂囦欢涓嬭浇鍑洪敊")); - WebHelper.write2Page(res, msg); - } catch (Exception e) { - log.error(e.getMessage(), e); + String filePath = pathHelper.getConfig().getUploadPath() + File.separator + entity.getPath(); + File file = new File(filePath); + if (!file.exists() || file.isDirectory()) { + WebHelper.writeJson2Page(res, "鏂囦欢涓嶅瓨鍦�"); } + + WebHelper.download(filePath, entity.getName(), inline, res); + } catch (Exception ex) { + WebHelper.writeJson2Page(res, "鏂囦欢涓嬭浇鍑洪敊"); log.error(ex.getMessage(), ex); } } @@ -191,7 +200,7 @@ for (MetaFileEntity mf : list) { if (mf.getName().contains(StaticData.ZIP)) { List<String> subs = getZipFiles(mf, path); - if (null == subs || subs.size() > 0) { + if (null != subs && subs.size() > 0) { files.addAll(subs); } continue; @@ -400,4 +409,90 @@ return entity; } + + /** + * 璇锋眰涓嬭浇 + */ + public String downloadReqForGuids(UserEntity ue, List<String> guids) { + List<AttachEntity> list = attachService.selectByGuids(guids); + + return downloadReq(ue, list); + } + + /** + * 璇锋眰涓嬭浇 + */ + public String downloadReqForTabGuids(UserEntity ue, String tab, List<String> guids) { + List<AttachEntity> list = attachService.selectByTabGuids(tab, guids); + + return downloadReq(ue, list); + } + + /** + * 璇锋眰涓嬭浇 + */ + protected String downloadReq(UserEntity ue, List<AttachEntity> list) { + if (null == list || list.isEmpty()) { + return null; + } + + 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; + } } -- Gitblit v1.9.3