| | |
| | | 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; |
| | |
| | | */ |
| | | @Service |
| | | public class BaseUploadService { |
| | | private final static String XLS = ".xls"; |
| | | |
| | | private final static String MDB = ".mdb"; |
| | | |
| | | private final static String GDB = ".gdb"; |
| | | |
| | | private final static String SHP_ZIP = ".shp.zip"; |
| | | |
| | | private final static String GDB_ZIP = ".gdb.zip"; |
| | | |
| | | private final static String ZIP = ".zip"; |
| | | @Autowired |
| | | private DirService dirService; |
| | | |
| | | @Autowired |
| | | public PathHelper pathHelper; |
| | | private PathHelper pathHelper; |
| | | |
| | | public final Log log = LogFactory.getLog(getClass()); |
| | | |
| | |
| | | mf.setExtName(extName); |
| | | mf.setSizes(FileHelper.sizeToMb(f.length())); |
| | | mf.setPath(subPath + File.separator + fileName); |
| | | // mf.setGuid(FileHelper.getFileMd5(f.getPath())) |
| | | if (!StaticData.ZIP.equals(extName)) { |
| | | mf.setGuid(FileHelper.getFileMd5(f.getPath())); |
| | | } |
| | | |
| | | list.add(mf); |
| | | } |
| | |
| | | |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | for (MetaFileEntity meta : metas) { |
| | | meta.setEventid(StringHelper.getGuid()); |
| | | meta.setDirid(dir.getId()); |
| | | meta.setDepid(ue.getDepid()); |
| | | meta.setVerid(ver.getId()); |
| | |
| | | meta.setEpsgCode(epsgCode); |
| | | |
| | | if (StaticData.ZIP.equals(meta.getExtName())) { |
| | | List<MetaFileEntity> subs = getMappers(path, meta.getPath(), meta); |
| | | List<MetaFileEntity> subs = getMapperFiles(path, dir, meta); |
| | | if (null != subs && subs.size() > 0) { |
| | | list.addAll(subs); |
| | | continue; |
| | | } |
| | | } |
| | | |
| | | meta.setEventid(StringHelper.getGuid()); |
| | | list.add(meta); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取映射 |
| | | * 获取映射文件 |
| | | */ |
| | | private List<MetaFileEntity> getMappers(String path, String zipPath, MetaFileEntity meta) { |
| | | File file = new File(pathHelper.getConfig().getTempPath() + File.separator + zipPath); |
| | | private List<MetaFileEntity> getMapperFiles(String path, DirEntity dir, MetaFileEntity meta) { |
| | | String zipFile = pathHelper.getConfig().getTempPath() + File.separator + meta.getPath(); |
| | | File file = new File(zipFile); |
| | | if (!file.exists() || file.isDirectory()) { |
| | | return null; |
| | | } |
| | |
| | | zipFolder.mkdirs(); |
| | | } |
| | | |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | String subPath = zipFolder + File.separator + meta.getName().toLowerCase().replace(".zip", ""); |
| | | ZipHelper.unzip(zipFile, subPath); |
| | | |
| | | File subFile = new File(subPath); |
| | | File[] files = subFile.listFiles(); |
| | | if (null == files || files.length == 0) { |
| | | return null; |
| | | } |
| | | |
| | | return list; |
| | | return getMapperFiles(files, dir, meta, subPath); |
| | | } |
| | | |
| | | /*private List<TabMapperEntity> getMappers(String zipPath, File[] files) { |
| | | String temp = pathHelper.getConfig().getTempPath(); |
| | | |
| | | List<TabMapperEntity> list = new ArrayList<>(); |
| | | /** |
| | | * 获取映射文件 |
| | | */ |
| | | private List<MetaFileEntity> getMapperFiles(File[] files, DirEntity dir, MetaFileEntity meta, String subPath) { |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | for (File f : files) { |
| | | String fileName = FileHelper.getFileName(f.getPath()); |
| | | if (fileName.contains(XLS)) { |
| | | String path = f.getPath().replace(temp + File.separator, ""); |
| | | list.add(new TabMapperEntity(fileName, "xls", null, path)); |
| | | boolean isGdb = isGdbFile(f); |
| | | if (!isGdb && f.isDirectory()) { |
| | | continue; |
| | | } |
| | | if (fileName.contains(MDB)) { |
| | | String path = f.getPath().replace(temp + File.separator, ""); |
| | | List<String> tabs = MdbHelper.getTabNames(f.getPath()); |
| | | for (String tab : tabs) { |
| | | list.add(new TabMapperEntity(fileName, "mdb", tab, path)); |
| | | |
| | | String fileName = FileHelper.getFileName(f.getPath()); |
| | | String extName = FileHelper.getExtension(fileName); |
| | | if (!StaticData.ALL_EXTENSION.contains(extName)) { |
| | | continue; |
| | | } |
| | | |
| | | int dirid = getDirByPath(f.getPath(), dir); |
| | | boolean isShp = StaticData.SHP.equals(extName); |
| | | if (isGdb) { |
| | | List<MetaFileEntity> rs = getGdbMappers(f, meta, dirid, subPath); |
| | | if (null != rs && rs.size() > 0) { |
| | | list.addAll(rs); |
| | | } |
| | | continue; |
| | | } |
| | | if (fileName.contains(SHP_ZIP)) { |
| | | String subPath = zipPath + File.separator + f.getName().toLowerCase().replace(".zip", ""); |
| | | ZipHelper.unzip(f.getPath(), subPath); |
| | | getShpFiles(f.getName(), subPath, list); |
| | | continue; |
| | | |
| | | MetaFileEntity mf = createMetaFileEntity(meta); |
| | | mf.setDirid(dirid); |
| | | mf.setEventid(StringHelper.getGuid()); |
| | | mf.setName(fileName); |
| | | mf.setExtName(extName); |
| | | mf.setPath(f.getPath().substring(subPath.length())); |
| | | |
| | | if (isShp) { |
| | | List<String> shpFiles = getShpFiles(f.getPath()); |
| | | mf.setTab(fileName); |
| | | mf.setSizes(getFilesSize(shpFiles)); |
| | | mf.setGuid(getFilesMd5(shpFiles)); |
| | | } else { |
| | | mf.setSizes(FileHelper.sizeToMb(f.length())); |
| | | mf.setGuid(FileHelper.getFileMd5(f.getPath())); |
| | | } |
| | | if (fileName.contains(GDB_ZIP)) { |
| | | String subPath = zipPath + File.separator + f.getName().toLowerCase().replace(".zip", ""); |
| | | ZipHelper.unzip(f.getPath(), subPath); |
| | | getGdbFiles(f.getName(), subPath, list); |
| | | continue; |
| | | list.add(mf); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 创建元数据文件实体类 |
| | | */ |
| | | private MetaFileEntity createMetaFileEntity(MetaFileEntity meta) { |
| | | MetaFileEntity mf = new MetaFileEntity(); |
| | | mf.setDirid(meta.getDirid()); |
| | | mf.setDepid(meta.getDepid()); |
| | | mf.setVerid(meta.getVerid()); |
| | | mf.setCreateUser(meta.getCreateUser()); |
| | | mf.setEpsgCode(meta.getEpsgCode()); |
| | | |
| | | return mf; |
| | | } |
| | | |
| | | /** |
| | | * 根据文件路径获取目录ID |
| | | */ |
| | | private int getDirByPath(String filePath, DirEntity dir) { |
| | | if (0 != dir.getPid()) { |
| | | return dir.getId(); |
| | | } |
| | | |
| | | List<DirEntity> list = dirService.selectDirRecursive(dir.getName()); |
| | | if (null == list || list.isEmpty()) { |
| | | return dir.getId(); |
| | | } |
| | | if ("/".equals(File.separator)) { |
| | | filePath = filePath.replace("/", "\\"); |
| | | } |
| | | |
| | | for (DirEntity entity : list) { |
| | | if (filePath.contains(entity.getFullName())) { |
| | | return entity.getId(); |
| | | } |
| | | if (fileName.contains(ZIP)) { |
| | | // 暂时不实现 |
| | | } |
| | | |
| | | return dir.getId(); |
| | | } |
| | | |
| | | /** |
| | | * 获取SHP文件集合 |
| | | */ |
| | | private List<String> getShpFiles(String shpPath) { |
| | | List<String> list = new ArrayList<>(); |
| | | list.add(shpPath); |
| | | |
| | | for (int i = 1, c = StaticData.SHP_EXTENSION.size(); i < c; i++) { |
| | | String path = shpPath.replace(".shp", StaticData.SHP_EXTENSION.get(i)); |
| | | |
| | | File f = new File(path); |
| | | if (f.exists() && !f.isDirectory()) { |
| | | list.add(path); |
| | | } |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | private void getShpFiles(String sourceName, String subPath, List<TabMapperEntity> list) { |
| | | List<String> files = new ArrayList<>(); |
| | | getShpFiles(subPath, files); |
| | | /** |
| | | * 获取GDB文件集合 |
| | | */ |
| | | private List<String> getGdbFiles(String path) { |
| | | List<String> list = new ArrayList<>(); |
| | | |
| | | String root = pathHelper.getConfig().getTempPath() + File.separator; |
| | | for (String file : files) { |
| | | String name = FileHelper.getFileName(file); |
| | | String path = file.replace(root, ""); |
| | | list.add(new TabMapperEntity(sourceName, "shp", name, path)); |
| | | } |
| | | } |
| | | |
| | | private void getShpFiles(String shpPath, List<String> list) { |
| | | File file = new File(shpPath); |
| | | |
| | | File[] files = file.listFiles(); |
| | | File[] files = new File(path).listFiles(); |
| | | if (null == files || files.length == 0) { |
| | | return; |
| | | return list; |
| | | } |
| | | |
| | | for (File f : files) { |
| | | if (f.isDirectory()) { |
| | | getShpFiles(f.getPath(), list); |
| | | continue; |
| | | } |
| | | |
| | | if (f.getName().toLowerCase().endsWith(".shp")) { |
| | | list.add(f.getPath()); |
| | | } |
| | | list.add(f.getPath()); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | private void getGdbFiles(String sourceName, String subPath, List<TabMapperEntity> list) { |
| | | List<String> files = new ArrayList<>(); |
| | | getGdbFiles(subPath, files); |
| | | |
| | | String root = pathHelper.getConfig().getTempPath() + File.separator; |
| | | /** |
| | | * 获取多文件的MD5 |
| | | */ |
| | | private String getFilesMd5(List<String> files) { |
| | | List<String> list = new ArrayList<>(); |
| | | for (String file : files) { |
| | | String path = file.replace(root, ""); |
| | | List<String> tabs = GdbHelper.getTabNames(file); |
| | | for (String tab : tabs) { |
| | | list.add(new TabMapperEntity(sourceName, "gdb", tab, path)); |
| | | 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 void getGdbFiles(String shpPath, List<String> list) { |
| | | File file = new File(shpPath); |
| | | |
| | | File[] files = file.listFiles(); |
| | | if (null == files || files.length == 0) { |
| | | return; |
| | | /** |
| | | * 获取多文件的大小 |
| | | */ |
| | | 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(); |
| | | } |
| | | } |
| | | |
| | | for (File f : files) { |
| | | if (!f.isDirectory()) { |
| | | continue; |
| | | } |
| | | return FileHelper.sizeToMb(size); |
| | | } |
| | | |
| | | if (isGdbFile(f)) { |
| | | list.add(f.getPath()); |
| | | continue; |
| | | } |
| | | |
| | | getGdbFiles(f.getPath(), list); |
| | | /** |
| | | * 获取GDB文件映射 |
| | | */ |
| | | private List<MetaFileEntity> getGdbMappers(File f, MetaFileEntity meta, int dirid, String subPath) { |
| | | 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.setDirid(dirid); |
| | | mf.setEventid(StringHelper.getGuid()); |
| | | mf.setName(fileName); |
| | | mf.setExtName(extName); |
| | | mf.setSizes(FileHelper.sizeToMb(f.length())); |
| | | mf.setPath(f.getPath().substring(subPath.length())); |
| | | mf.setTab(tab); |
| | | mf.setSizes(sizes); |
| | | mf.setGuid(md5); |
| | | list.add(mf); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 是/否为GDB文件 |
| | | */ |
| | | private boolean isGdbFile(File f) { |
| | | if (f.getName().toLowerCase().endsWith(GDB)) { |
| | | if (f.isDirectory() && f.getName().toLowerCase().endsWith(StaticData.GDB)) { |
| | | File[] files = f.listFiles(); |
| | | if (null == files || files.length == 0) { |
| | | return false; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取参数 |
| | | * 获取参数 * |
| | | * Enumeration<String> headers = req.getHeaderNames(); |
| | | * Enumeration<String> attributes = req.getAttributeNames(); |
| | | */ |