| | |
| | | import com.lf.server.entity.sys.UserEntity; |
| | | import com.lf.server.helper.*; |
| | | import com.lf.server.service.data.DirService; |
| | | import com.lf.server.service.sys.TokenService; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | |
| | | */ |
| | | @Service |
| | | public class BaseUploadService { |
| | | @Autowired |
| | | private DirService dirService; |
| | | @Resource |
| | | protected DirService dirService; |
| | | |
| | | @Autowired |
| | | private PathHelper pathHelper; |
| | | @Resource |
| | | protected PathHelper pathHelper; |
| | | |
| | | @Resource |
| | | protected TokenService tokenService; |
| | | |
| | | public final Log log = LogFactory.getLog(getClass()); |
| | | |
| | |
| | | /** |
| | | * 上传文件 |
| | | */ |
| | | public <T> List<MetaFileEntity> uploadData(T t, String path, HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | public <T> List<MetaFileEntity> uploadData(T t, String path, boolean isGetGuid, HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | StandardMultipartHttpServletRequest request = (StandardMultipartHttpServletRequest) req; |
| | | req.setCharacterEncoding("utf-8"); |
| | | res.setContentType("application/json;charset=utf-8"); |
| | |
| | | setEntity(t, request); |
| | | } |
| | | |
| | | return getFiles(path, request); |
| | | return getFiles(path, isGetGuid, request); |
| | | } |
| | | |
| | | /** |
| | |
| | | /** |
| | | * 获取文件 |
| | | */ |
| | | public List<MetaFileEntity> getFiles(String subPath, StandardMultipartHttpServletRequest req) throws Exception { |
| | | public List<MetaFileEntity> getFiles(String subPath, boolean isGetGuid, StandardMultipartHttpServletRequest req) throws Exception { |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | |
| | | String path = pathHelper.getTempPath(subPath); |
| | |
| | | mf.setPath(path + File.separator + mf.getName()); |
| | | |
| | | file.transferTo(new File(mf.getPath())); |
| | | mf.setGuid(FileHelper.getFileMd5(mf.getPath())); |
| | | if (isGetGuid) { |
| | | mf.setGuid(FileHelper.getFileMd5(mf.getPath())); |
| | | } |
| | | |
| | | list.add(mf); |
| | | } |
| | |
| | | /** |
| | | * 查询文件 |
| | | */ |
| | | public List<MetaFileEntity> selectFiles(String subPath, List<String> extList) { |
| | | public List<MetaFileEntity> selectFiles(String subPath, boolean hasMd5) { |
| | | String root = pathHelper.getConfig().getTempPath() + File.separator + subPath; |
| | | |
| | | File file = new File(root); |
| | |
| | | for (File f : files) { |
| | | String fileName = FileHelper.getFileName(f.getPath()); |
| | | String extName = FileHelper.getExtension(fileName); |
| | | if (null != extList && !extList.contains(extName)) { |
| | | if (!isFileValid(f.getName())) { |
| | | continue; |
| | | } |
| | | |
| | |
| | | mf.setExtName(extName); |
| | | mf.setSizes(FileHelper.sizeToMb(f.length())); |
| | | mf.setPath(subPath + File.separator + fileName); |
| | | if (!StaticData.ZIP.equals(extName)) { |
| | | if (hasMd5 && !StaticData.ZIP.equals(extName)) { |
| | | mf.setGuid(FileHelper.getFileMd5(f.getPath())); |
| | | } |
| | | |
| | |
| | | * 查询映射 |
| | | */ |
| | | public List<MetaFileEntity> selectMappers(UserEntity ue, String path, DirEntity dir, VerEntity ver, String epsgCode) { |
| | | List<MetaFileEntity> metas = selectFiles(path, StaticData.ALL_EXTENSION); |
| | | List<MetaFileEntity> metas = selectFiles(path, true); |
| | | 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.setDirid(dir.getId()); |
| | | meta.setDepid(ue.getDepid()); |
| | | 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); |
| | | List<MetaFileEntity> subs = getMapperFiles(path, meta); |
| | | if (null != subs && subs.size() > 0) { |
| | | list.addAll(subs); |
| | | continue; |
| | | } |
| | | } |
| | | |
| | | list.add(meta); |
| | | } |
| | | setMetaType(list); |
| | | setDircode(list, dir); |
| | | |
| | | return list; |
| | | } |
| | |
| | | /** |
| | | * 获取映射文件 |
| | | */ |
| | | private List<MetaFileEntity> getMapperFiles(String path, DirEntity dir, List<DirEntity> dirs, MetaFileEntity meta) { |
| | | private List<MetaFileEntity> getMapperFiles(String path, MetaFileEntity meta) { |
| | | String tempPath = pathHelper.getConfig().getTempPath(); |
| | | String zipFile = tempPath + File.separator + meta.getPath(); |
| | | File file = new File(zipFile); |
| | |
| | | zipFolder.mkdirs(); |
| | | } |
| | | |
| | | String subPath = zipFolder + File.separator + meta.getName().toLowerCase().replace(".zip", ""); |
| | | // String subPath = zipFolder + File.separator + meta.getName().toLowerCase().replace(".zip", "") .replace("(","").replace(")","").replace(" ","") |
| | | String subPath = zipFolder.getPath(); |
| | | ZipHelper.unzip(zipFile, subPath); |
| | | |
| | | List<File> files = new ArrayList<>(); |
| | | FileHelper.getFilesByPath(files, subPath); |
| | | getFilesByPath(files, subPath); |
| | | |
| | | return getMapperFiles(files, dir, dirs, meta, tempPath.length() + 1); |
| | | return getMapperFiles(files, meta, tempPath.length() + 1); |
| | | } |
| | | |
| | | /** |
| | | * 根据路径获取文件 |
| | | */ |
| | | private void getFilesByPath(List<File> list, String path) { |
| | | File file = new File(path); |
| | | if (!file.isDirectory()) { |
| | | if (isFileValid(file.getName())) { |
| | | list.add(file); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | if (isGdbFile(file) || isOsgbFile(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 { |
| | | if (isFileValid(f.getName())) { |
| | | list.add(f); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 文件是/否有效 |
| | | */ |
| | | private boolean isFileValid(String fileName) { |
| | | String extName = FileHelper.getExtension(fileName); |
| | | if (fileName.startsWith(StaticData.TILDE) || !StaticData.ALL_EXTENSION.contains(extName)) { |
| | | return false; |
| | | } |
| | | |
| | | fileName = fileName.toLowerCase(); |
| | | for (String ext : StaticData.MAPPER_EXCLUDE_EXT) { |
| | | if (fileName.contains(ext)) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 获取映射文件 |
| | | */ |
| | | private List<MetaFileEntity> getMapperFiles(List<File> files, DirEntity dir, List<DirEntity> dirs, MetaFileEntity meta, int start) { |
| | | private List<MetaFileEntity> getMapperFiles(List<File> files, MetaFileEntity meta, int start) { |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | for (File f : files) { |
| | | boolean isGdb = isGdbFile(f); |
| | | if (!isGdb && f.isDirectory()) { |
| | | boolean isOsgb = isOsgbFile(f); |
| | | boolean isFile = isGdb || isOsgb; |
| | | if (f.isDirectory() && !isFile) { |
| | | continue; |
| | | } |
| | | |
| | | String fileName = FileHelper.getFileName(f.getPath()); |
| | | String extName = FileHelper.getExtension(fileName); |
| | | if (!StaticData.ALL_EXTENSION.contains(extName)) { |
| | | String extName = isOsgb ? StaticData.OSGB : FileHelper.getExtension(fileName); |
| | | if (!isFileValid(f.getName())) { |
| | | continue; |
| | | } |
| | | |
| | | int dirid = getDirByPath(f.getPath(), dir, dirs); |
| | | boolean isShp = StaticData.SHP.equals(extName); |
| | | if (isGdb) { |
| | | List<MetaFileEntity> rs = getGdbMappers(f, meta, dirid, start); |
| | | List<MetaFileEntity> rs = getGdbMappers(f, meta, start); |
| | | if (null != rs && rs.size() > 0) { |
| | | list.addAll(rs); |
| | | } |
| | | continue; |
| | | } |
| | | |
| | | MetaFileEntity mf = createMetaFileEntity(meta); |
| | | mf.setDirid(dirid); |
| | | 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); |
| | | mf.setSizes(getFilesSize(shpFiles)); |
| | | mf.setGuid(getFilesMd5(shpFiles)); |
| | | } else { |
| | | mf.setSizes(FileHelper.sizeToMb(f.length())); |
| | | mf.setGuid(FileHelper.getFileMd5(f.getPath())); |
| | | if (isOsgb) { |
| | | list.add(getOsgbMapper(f, meta, start)); |
| | | continue; |
| | | } |
| | | |
| | | MetaFileEntity mf = createMetaFileEntity(f, meta, fileName, extName); |
| | | mf.setPath(f.getPath().substring(start)); |
| | | list.add(mf); |
| | | } |
| | | |
| | |
| | | /** |
| | | * 创建元数据文件实体类 |
| | | */ |
| | | private MetaFileEntity createMetaFileEntity(File f, MetaFileEntity meta, String fileName, String extName) { |
| | | boolean isShp = StaticData.SHP.equals(extName); |
| | | |
| | | MetaFileEntity mf = createMetaFileEntity(meta); |
| | | mf.setEventid(StringHelper.getGuid()); |
| | | mf.setName(fileName); |
| | | mf.setExtName(extName); |
| | | |
| | | 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())); |
| | | } |
| | | |
| | | return mf; |
| | | } |
| | | |
| | | /** |
| | | * 创建元数据文件实体类 |
| | | */ |
| | | private MetaFileEntity createMetaFileEntity(MetaFileEntity meta) { |
| | | MetaFileEntity mf = new MetaFileEntity(); |
| | | mf.setDirid(meta.getDirid()); |
| | | mf.setDepid(meta.getDepid()); |
| | | mf.setDepcode(meta.getDepcode()); |
| | | mf.setVerid(meta.getVerid()); |
| | | mf.setCreateUser(meta.getCreateUser()); |
| | | mf.setEpsgCode(meta.getEpsgCode()); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 根据文件路径获取目录ID |
| | | */ |
| | | private int getDirByPath(String filePath, DirEntity dir, List<DirEntity> dirs) { |
| | | if (0 != dir.getPid() || null == dirs || dirs.isEmpty()) { |
| | | return dir.getId(); |
| | | } |
| | | if ("/".equals(File.separator)) { |
| | | filePath = filePath.replace("/", "\\"); |
| | | } |
| | | |
| | | for (DirEntity entity : dirs) { |
| | | if (filePath.contains(entity.getFullName())) { |
| | | return entity.getId(); |
| | | } |
| | | } |
| | | |
| | | return dir.getId(); |
| | | } |
| | | |
| | | /** |
| | | * 获取SHP文件集合 |
| | | */ |
| | | private List<String> getShpFiles(String shpPath) { |
| | | 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)); |
| | | 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()) { |
| | |
| | | /** |
| | | * 获取GDB文件映射 |
| | | */ |
| | | private List<MetaFileEntity> getGdbMappers(File f, MetaFileEntity meta, int dirid, int start) { |
| | | private List<MetaFileEntity> getGdbMappers(File f, MetaFileEntity meta, int start) { |
| | | List<String> tabs = GdbHelper.getTabNames(f.getPath()); |
| | | if (null == tabs || tabs.size() == 0) { |
| | | return null; |
| | |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | for (String tab : tabs) { |
| | | MetaFileEntity mf = createMetaFileEntity(meta); |
| | | mf.setDirid(dirid); |
| | | mf.setEventid(StringHelper.getGuid()); |
| | | mf.setName(fileName); |
| | | mf.setName(fileName + "\\" + tab); |
| | | mf.setExtName(extName); |
| | | mf.setSizes(FileHelper.sizeToMb(f.length())); |
| | | mf.setPath(f.getPath().substring(start)); |
| | |
| | | */ |
| | | 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; |
| | | } |
| | | File gdb = new File(f.getPath() + File.separator + "gdb"); |
| | | |
| | | for (File file : files) { |
| | | if ("gdb".equals(file.getName())) { |
| | | return true; |
| | | } |
| | | } |
| | | return gdb.exists() && !gdb.isDirectory(); |
| | | } |
| | | |
| | | 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, "")); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 是/否为OSGB文件 |
| | | */ |
| | | private boolean isOsgbFile(File f) { |
| | | if (f.isDirectory()) { |
| | | File meta = new File(f.getPath() + File.separator + "metadata.xml"); |
| | | File data = new File(f.getPath() + File.separator + "Data"); |
| | | |
| | | return meta.exists() && !meta.isDirectory() && data.exists() && data.isDirectory(); |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 获取OSGB文件映射 |
| | | */ |
| | | private MetaFileEntity getOsgbMapper(File f, MetaFileEntity meta, int start) { |
| | | String fileName = FileHelper.getFileName(f.getPath()); |
| | | |
| | | List<String> files = new ArrayList<>(); |
| | | FileHelper.getFilesByPath(files, f.getPath()); |
| | | |
| | | String md5 = getFilesMd5(files); |
| | | double sizes = getFilesSize(files); |
| | | |
| | | MetaFileEntity mf = createMetaFileEntity(meta); |
| | | mf.setEventid(StringHelper.getGuid()); |
| | | mf.setName(fileName); |
| | | mf.setExtName(StaticData.OSGB); |
| | | mf.setSizes(FileHelper.sizeToMb(f.length())); |
| | | mf.setPath(f.getPath().substring(start)); |
| | | mf.setSizes(sizes); |
| | | mf.setGuid(md5); |
| | | |
| | | return mf; |
| | | } |
| | | |
| | | /** |
| | | * 设置目录编码 |
| | | */ |
| | | private void setDircode(List<MetaFileEntity> list, DirEntity dir) { |
| | | List<DirEntity> dirs = dirService.selectRecursiveById(dir.getId()); |
| | | for (MetaFileEntity mfe : list) { |
| | | String filePath = mfe.getPath().toLowerCase().replace("/", "\\"); |
| | | String fileName = mfe.getName().toLowerCase(); |
| | | |
| | | String code = findDirByPath(dirs, filePath, fileName); |
| | | if (StringHelper.isEmpty(code)) { |
| | | DirEntity baseDir = findBaseDirByPath(dirs, filePath); |
| | | if (null != baseDir) { |
| | | createDirByPath(baseDir, mfe.getPath().replace("/", "\\")); |
| | | dirs = dirService.selectRecursiveById(dir.getId()); |
| | | code = findDirByPath(dirs, filePath, fileName); |
| | | } |
| | | } |
| | | code = StringHelper.isEmpty(code) ? dir.getCode() : code; |
| | | mfe.setDircode(code); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据路径查找目录 |
| | | */ |
| | | private String findDirByPath(List<DirEntity> dirs, String filePath,String fileName) { |
| | | for (DirEntity de : dirs) { |
| | | if (filePath.contains(de.getFullName().toLowerCase() + "\\" + fileName)) { |
| | | return de.getCode(); |
| | | } |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 根据路径查找父目录 |
| | | */ |
| | | private DirEntity findBaseDirByPath(List<DirEntity> dirs, String filePath) { |
| | | for (DirEntity de : dirs) { |
| | | if (StringHelper.isEmpty(de.getCode()) || de.getCode().length() != StaticData.FOUR) { |
| | | continue; |
| | | } |
| | | if (filePath.contains(de.getFullName().toLowerCase() + "\\")) { |
| | | return de; |
| | | } |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 根据路径创建目录 |
| | | */ |
| | | private void createDirByPath(DirEntity baseDir, String filePath) { |
| | | int start = filePath.toLowerCase().indexOf(baseDir.getFullName().toLowerCase() + "\\") + baseDir.getFullName().length() + 1; |
| | | int end = filePath.lastIndexOf("\\"); |
| | | if (end <= start) { |
| | | return; |
| | | } |
| | | if (0 == baseDir.getLevel()) { |
| | | baseDir.setLevel(2); |
| | | } |
| | | |
| | | HttpServletRequest req = WebHelper.getRequest(); |
| | | UserEntity ue = tokenService.getCurrentUser(req); |
| | | |
| | | String[] strs = filePath.substring(start, end).split("\\\\"); |
| | | for (String str : strs) { |
| | | DirEntity entity = dirService.selectDirByName(str, baseDir.getId()); |
| | | if (null == entity) { |
| | | entity = new DirEntity(); |
| | | entity.setPid(baseDir.getId()); |
| | | entity.setName(str); |
| | | entity.setOrderNum(dirService.selectMaxOrderNum()); |
| | | entity.setLevel(baseDir.getLevel() + 1); |
| | | entity.setCreateUser(ue.getId()); |
| | | |
| | | dirService.insert(entity); |
| | | baseDir = dirService.selectDir(entity.getId()); |
| | | } else { |
| | | baseDir = entity; |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | * Enumeration<String> attributes = req.getAttributeNames(); |
| | | */ |
| | | public Map<String, String> getParams(StandardMultipartHttpServletRequest req) { |
| | | Map<String, String> map = new HashMap<String, String>(3); |
| | | Map<String, String> map = new HashMap<>(3); |
| | | |
| | | Enumeration<String> enumeration = req.getParameterNames(); |
| | | while (enumeration.hasMoreElements()) { |