| | |
| | | package com.lf.server.service.all; |
| | | |
| | | import com.lf.server.entity.all.StaticData; |
| | | import com.lf.server.entity.data.DirEntity; |
| | | import com.lf.server.entity.data.MetaFileEntity; |
| | | import com.lf.server.helper.FileHelper; |
| | | import com.lf.server.helper.PathHelper; |
| | | import com.lf.server.helper.StringHelper; |
| | | import com.lf.server.entity.data.VerEntity; |
| | | import com.lf.server.entity.sys.UserEntity; |
| | | import com.lf.server.helper.*; |
| | | import com.lf.server.service.data.DirService; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.lang.reflect.Field; |
| | | import java.sql.Timestamp; |
| | | import java.util.*; |
| | |
| | | * 父上传服务 |
| | | * @author WWW |
| | | */ |
| | | @Service |
| | | public class BaseUploadService { |
| | | @Autowired |
| | | PathHelper pathHelper; |
| | | protected DirService dirService; |
| | | |
| | | private final Log log = LogFactory.getLog(getClass()); |
| | | @Autowired |
| | | protected PathHelper pathHelper; |
| | | |
| | | public final Log log = LogFactory.getLog(getClass()); |
| | | |
| | | /** |
| | | * 查询目录 |
| | | */ |
| | | public String selectPath() { |
| | | return pathHelper.getTempPathName(); |
| | | } |
| | | |
| | | /** |
| | | * 查询文件 |
| | | */ |
| | | public List<MetaFileEntity> selectFiles(String subPath) throws IOException { |
| | | String root = pathHelper.getConfig().getTempPath() + File.separator + subPath; |
| | | |
| | | File file = new File(root); |
| | | if (!file.exists() && !file.isDirectory()) { |
| | | return null; |
| | | } |
| | | File[] files = file.listFiles(); |
| | | if (null == files || files.length == 0) { |
| | | return null; |
| | | } |
| | | |
| | | List<MetaFileEntity> list = new ArrayList<MetaFileEntity>(); |
| | | for (File f : files) { |
| | | String fileName = FileHelper.getFileName(f.getPath()); |
| | | double sizes = FileHelper.sizeToMb(f.length()); |
| | | String filePath = subPath + File.separator + fileName; |
| | | |
| | | MetaFileEntity mf = new MetaFileEntity(); |
| | | mf.setName(fileName); |
| | | mf.setSizes(sizes); |
| | | mf.setPath(filePath); |
| | | mf.setGuid(FileHelper.getFileMd5(f.getPath())); |
| | | |
| | | list.add(mf); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | |
| | | if (t != null) { |
| | | setEntity(t, request); |
| | | } |
| | | List<MetaFileEntity> list = getFiles(path, request); |
| | | |
| | | return list; |
| | | return getFiles(path, request); |
| | | } |
| | | |
| | | /** |
| | |
| | | * 获取文件 |
| | | */ |
| | | public List<MetaFileEntity> getFiles(String subPath, StandardMultipartHttpServletRequest req) throws Exception { |
| | | List<MetaFileEntity> list = new ArrayList<MetaFileEntity>(); |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | |
| | | String path = pathHelper.getTempPath(subPath); |
| | | Iterator<String> iterator = req.getFileNames(); |
| | | while (iterator.hasNext()) { |
| | | MultipartFile file = req.getFile(iterator.next()); |
| | | if (StringHelper.isEmpty(file.getOriginalFilename())) { |
| | | if (null == file || StringHelper.isEmpty(file.getOriginalFilename())) { |
| | | continue; |
| | | } |
| | | |
| | | double sizes = FileHelper.sizeToMb(file.getSize()); |
| | | MetaFileEntity mf = new MetaFileEntity(); |
| | | mf.setName(file.getOriginalFilename()); |
| | | |
| | | double sizes = FileHelper.sizeToMb(file.getSize()); |
| | | mf.setSizes(sizes); |
| | | mf.setPath(path + File.separator + mf.getName()); |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 自理实体集合 |
| | | * 查询文件 |
| | | */ |
| | | public void dealEntities(List<MetaFileEntity> list) { |
| | | for (MetaFileEntity entity : list) { |
| | | entity.setPath(FileHelper.getRelativePath(entity.getPath())); |
| | | public List<MetaFileEntity> selectFiles(String subPath, List<String> extList) { |
| | | String root = pathHelper.getConfig().getTempPath() + File.separator + subPath; |
| | | |
| | | File file = new File(root); |
| | | if (!file.exists() && !file.isDirectory()) { |
| | | return null; |
| | | } |
| | | File[] files = file.listFiles(); |
| | | if (null == files || files.length == 0) { |
| | | return null; |
| | | } |
| | | |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | for (File f : files) { |
| | | String fileName = FileHelper.getFileName(f.getPath()); |
| | | String extName = FileHelper.getExtension(fileName); |
| | | if (null != extList && !extList.contains(extName)) { |
| | | continue; |
| | | } |
| | | |
| | | MetaFileEntity mf = new MetaFileEntity(); |
| | | mf.setName(fileName); |
| | | mf.setExtName(extName); |
| | | mf.setSizes(FileHelper.sizeToMb(f.length())); |
| | | mf.setPath(subPath + File.separator + fileName); |
| | | if (!StaticData.ZIP.equals(extName)) { |
| | | mf.setGuid(FileHelper.getFileMd5(f.getPath())); |
| | | } |
| | | |
| | | list.add(mf); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取参数 |
| | | * 查询映射 |
| | | */ |
| | | public List<MetaFileEntity> selectMappers(UserEntity ue, String path, DirEntity dir, VerEntity ver, String epsgCode) { |
| | | List<MetaFileEntity> metas = selectFiles(path, StaticData.ALL_EXTENSION); |
| | | if (null == metas || metas.isEmpty()) { |
| | | return null; |
| | | } |
| | | |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | List<DirEntity> dirs = dirService.selectDirRecursive(dir.getName()); |
| | | for (MetaFileEntity meta : metas) { |
| | | meta.setEventid(StringHelper.getGuid()); |
| | | meta.setDircode(dir.getCode()); |
| | | meta.setDepcode(ue.getDepcode()); |
| | | meta.setVerid(ver.getId()); |
| | | meta.setCreateUser(ue.getId()); |
| | | meta.setEpsgCode(epsgCode); |
| | | |
| | | if (StaticData.ZIP.equals(meta.getExtName())) { |
| | | List<MetaFileEntity> subs = getMapperFiles(path, dir, dirs, meta); |
| | | if (null != subs && subs.size() > 0) { |
| | | list.addAll(subs); |
| | | continue; |
| | | } |
| | | } |
| | | |
| | | list.add(meta); |
| | | } |
| | | setMetaType(list); |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 获取映射文件 |
| | | */ |
| | | private List<MetaFileEntity> getMapperFiles(String path, DirEntity dir, List<DirEntity> dirs, MetaFileEntity meta) { |
| | | String tempPath = pathHelper.getConfig().getTempPath(); |
| | | String zipFile = tempPath + File.separator + meta.getPath(); |
| | | File file = new File(zipFile); |
| | | if (!file.exists() || file.isDirectory()) { |
| | | return null; |
| | | } |
| | | |
| | | File zipFolder = new File(pathHelper.getConfig().getTempPath() + File.separator + path + "_zip"); |
| | | if (!zipFolder.exists() || !zipFolder.isDirectory()) { |
| | | zipFolder.mkdirs(); |
| | | } |
| | | |
| | | String subPath = zipFolder + File.separator + meta.getName().toLowerCase().replace(".zip", ""); |
| | | ZipHelper.unzip(zipFile, subPath); |
| | | |
| | | List<File> files = new ArrayList<>(); |
| | | getFilesByPath(files, subPath); |
| | | |
| | | return getMapperFiles(files, dir, dirs, meta, tempPath.length() + 1); |
| | | } |
| | | |
| | | /** |
| | | * 根据路径获取文件 |
| | | */ |
| | | private void getFilesByPath(List<File> list, String path) { |
| | | File file = new File(path); |
| | | if (!file.isDirectory()) { |
| | | String extName = FileHelper.getExtension(file); |
| | | if (StaticData.ALL_EXTENSION.contains(extName)) { |
| | | list.add(file); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | if (isGdbFile(file)) { |
| | | list.add(file); |
| | | return; |
| | | } |
| | | |
| | | File[] files = file.listFiles(); |
| | | if (null == files) { |
| | | return; |
| | | } |
| | | for (File f : files) { |
| | | if (f.isDirectory()) { |
| | | getFilesByPath(list, f.getPath()); |
| | | } else { |
| | | String extName = FileHelper.getExtension(f); |
| | | if (StaticData.ALL_EXTENSION.contains(extName)) { |
| | | list.add(f); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取映射文件 |
| | | */ |
| | | private List<MetaFileEntity> getMapperFiles(List<File> files, DirEntity dir, List<DirEntity> dirs, MetaFileEntity meta, int start) { |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | for (File f : files) { |
| | | boolean isGdb = isGdbFile(f); |
| | | if (!isGdb && f.isDirectory()) { |
| | | continue; |
| | | } |
| | | |
| | | String fileName = FileHelper.getFileName(f.getPath()); |
| | | String extName = FileHelper.getExtension(fileName); |
| | | if (!StaticData.ALL_EXTENSION.contains(extName) || fileName.startsWith("~")) { |
| | | continue; |
| | | } |
| | | |
| | | String dircode = getDirByPath(f.getPath(), fileName, dir, dirs); |
| | | boolean isShp = StaticData.SHP.equals(extName); |
| | | if (isGdb) { |
| | | List<MetaFileEntity> rs = getGdbMappers(f, meta, dircode, start); |
| | | if (null != rs && rs.size() > 0) { |
| | | list.addAll(rs); |
| | | } |
| | | continue; |
| | | } |
| | | |
| | | MetaFileEntity mf = createMetaFileEntity(meta); |
| | | mf.setDircode(dircode); |
| | | mf.setEventid(StringHelper.getGuid()); |
| | | mf.setName(fileName); |
| | | mf.setExtName(extName); |
| | | mf.setPath(f.getPath().substring(start)); |
| | | |
| | | if (isShp) { |
| | | List<String> shpFiles = getShpFiles(f.getPath()); |
| | | mf.setTab(fileName.replace(StaticData.SHP, "")); |
| | | mf.setSizes(getFilesSize(shpFiles)); |
| | | mf.setGuid(getFilesMd5(shpFiles)); |
| | | } else { |
| | | mf.setSizes(FileHelper.sizeToMb(f.length())); |
| | | mf.setGuid(FileHelper.getFileMd5(f.getPath())); |
| | | } |
| | | list.add(mf); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 创建元数据文件实体类 |
| | | */ |
| | | private MetaFileEntity createMetaFileEntity(MetaFileEntity meta) { |
| | | MetaFileEntity mf = new MetaFileEntity(); |
| | | mf.setDircode(meta.getDircode()); |
| | | mf.setDepcode(meta.getDepcode()); |
| | | mf.setVerid(meta.getVerid()); |
| | | mf.setCreateUser(meta.getCreateUser()); |
| | | mf.setEpsgCode(meta.getEpsgCode()); |
| | | |
| | | return mf; |
| | | } |
| | | |
| | | /** |
| | | * 根据文件路径获取目录ID |
| | | */ |
| | | private String getDirByPath(String filePath,String fileName, DirEntity dir, List<DirEntity> dirs) { |
| | | if (0 != dir.getPid() || null == dirs || dirs.isEmpty()) { |
| | | return dir.getCode(); |
| | | } |
| | | if (StaticData.SLASH.equals(File.separator)) { |
| | | filePath = filePath.replace("/", "\\"); |
| | | } |
| | | |
| | | for (DirEntity entity : dirs) { |
| | | if (filePath.contains(entity.getFullName() + "\\" + fileName)) { |
| | | return entity.getCode(); |
| | | } |
| | | } |
| | | |
| | | return dir.getCode(); |
| | | } |
| | | |
| | | /** |
| | | * 获取SHP文件集合 |
| | | */ |
| | | private List<String> getShpFiles(String shpPath) { |
| | | List<String> list = new ArrayList<>(); |
| | | list.add(shpPath); |
| | | |
| | | for (int i = 0, c = StaticData.SHP_EXT.size(); i < c; i++) { |
| | | String path = shpPath.replace(".shp", StaticData.SHP_EXT.get(i)); |
| | | |
| | | File f = new File(path); |
| | | if (f.exists() && !f.isDirectory()) { |
| | | list.add(path); |
| | | } |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 获取GDB文件集合 |
| | | */ |
| | | private List<String> getGdbFiles(String path) { |
| | | List<String> list = new ArrayList<>(); |
| | | |
| | | File[] files = new File(path).listFiles(); |
| | | if (null == files || files.length == 0) { |
| | | return list; |
| | | } |
| | | for (File f : files) { |
| | | list.add(f.getPath()); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 获取多文件的MD5 |
| | | */ |
| | | private String getFilesMd5(List<String> files) { |
| | | List<String> list = new ArrayList<>(); |
| | | for (String file : files) { |
| | | String md5 = FileHelper.getFileMd5(file); |
| | | if (null != md5) { |
| | | list.add(md5); |
| | | } |
| | | } |
| | | |
| | | if (list.size() > 0) { |
| | | String str = StringHelper.join(list, ","); |
| | | return FileHelper.getStringMd5(str); |
| | | } |
| | | |
| | | return StringHelper.getGuid(); |
| | | } |
| | | |
| | | /** |
| | | * 获取多文件的大小 |
| | | */ |
| | | private double getFilesSize(List<String> files) { |
| | | long size = 0L; |
| | | for (String file : files) { |
| | | File f = new File(file); |
| | | if (f.exists() && !f.isDirectory()) { |
| | | size += f.length(); |
| | | } |
| | | } |
| | | |
| | | return FileHelper.sizeToMb(size); |
| | | } |
| | | |
| | | /** |
| | | * 获取GDB文件映射 |
| | | */ |
| | | private List<MetaFileEntity> getGdbMappers(File f, MetaFileEntity meta, String dircode, int start) { |
| | | List<String> tabs = GdbHelper.getTabNames(f.getPath()); |
| | | if (null == tabs || tabs.size() == 0) { |
| | | return null; |
| | | } |
| | | |
| | | String fileName = FileHelper.getFileName(f.getPath()); |
| | | String extName = FileHelper.getExtension(fileName); |
| | | |
| | | List<String> gdbFiles = getGdbFiles(f.getPath()); |
| | | String md5 = getFilesMd5(gdbFiles); |
| | | double sizes = getFilesSize(gdbFiles); |
| | | |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | for (String tab : tabs) { |
| | | MetaFileEntity mf = createMetaFileEntity(meta); |
| | | mf.setDircode(dircode); |
| | | mf.setEventid(StringHelper.getGuid()); |
| | | mf.setName(fileName); |
| | | mf.setExtName(extName); |
| | | mf.setSizes(FileHelper.sizeToMb(f.length())); |
| | | mf.setPath(f.getPath().substring(start)); |
| | | mf.setTab(tab); |
| | | mf.setSizes(sizes); |
| | | mf.setGuid(md5); |
| | | list.add(mf); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 是/否为GDB文件 |
| | | */ |
| | | private boolean isGdbFile(File f) { |
| | | if (f.isDirectory() && f.getName().toLowerCase().endsWith(StaticData.GDB)) { |
| | | File[] files = f.listFiles(); |
| | | if (null == files || files.length == 0) { |
| | | return false; |
| | | } |
| | | |
| | | for (File file : files) { |
| | | if ("gdb".equals(file.getName())) { |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 设置元数据文件的类型 |
| | | */ |
| | | private void setMetaType(List<MetaFileEntity> list) { |
| | | for (MetaFileEntity mf : list) { |
| | | if (null != mf.getExtName()) { |
| | | mf.setType(mf.getExtName().replace(".", "")); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理目录 |
| | | */ |
| | | public void copePath(List<MetaFileEntity> list) { |
| | | String basePath = pathHelper.getConfig().getTempPath() + File.separator; |
| | | for (MetaFileEntity mf : list) { |
| | | mf.setPath(mf.getPath().replace(basePath, "")); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取参数 * |
| | | * Enumeration<String> headers = req.getHeaderNames(); |
| | | * Enumeration<String> attributes = req.getAttributeNames(); |
| | | */ |