| | |
| | | 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.io.FileUtils; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | 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.*; |
| | | |
| | | /** |
| | | * 上传附件服务类 |
| | |
| | | @Autowired |
| | | AttachService attachService; |
| | | |
| | | @Autowired |
| | | DownloadMapper downloadMapper; |
| | | |
| | | 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); |
| | | |
| | | private static final String NO_FILE = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.NOT_FOUND, "文件找不到")); |
| | | |
| | | /** |
| | | * 获取表名 |
| | |
| | | * 下载文件 |
| | | */ |
| | | public void download(String guid, HttpServletResponse res) { |
| | | download(guid, res); |
| | | download(guid, false, res); |
| | | } |
| | | |
| | | /** |
| | |
| | | public void download(String guid, boolean inline, HttpServletResponse res) { |
| | | try { |
| | | if (StringHelper.isEmpty(guid)) { |
| | | WebHelper.writeStr2Page(res, NO_FILE); |
| | | WebHelper.writeStr2Page(res, StaticData.NO_FILE); |
| | | return; |
| | | } |
| | | |
| | | AttachEntity entity = attachService.selectByGuid(guid); |
| | | if (entity == null) { |
| | | WebHelper.writeStr2Page(res, NO_FILE); |
| | | WebHelper.writeStr2Page(res, StaticData.NO_FILE); |
| | | return; |
| | | } |
| | | |
| | |
| | | |
| | | 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(","); |
| | | } |
| | | |
| | | /** |
| | |
| | | String md5 = FileHelper.getFileMd5(file); |
| | | AttachEntity old = attachService.selectByTabAndGuid(meta.getTab(), be.getEventid(), md5); |
| | | if (null != old) { |
| | | f.delete(); |
| | | return 0; |
| | | } |
| | | |
| | |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | 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; |
| | | } |
| | | } |