|
@@ -0,0 +1,408 @@
|
|
|
+package com.onemap.file.service;
|
|
|
+
|
|
|
+import com.google.common.collect.ImmutableMap;
|
|
|
+import com.onemap.common.core.domain.TgclTypeDTO;
|
|
|
+import com.onemap.common.core.domain.UploadZipDTO;
|
|
|
+import com.onemap.common.core.domain.WordToPdfDTO;
|
|
|
+import com.onemap.common.core.utils.StringUtils;
|
|
|
+import com.onemap.common.core.web.domain.RequestResult;
|
|
|
+import com.onemap.file.domain.ShpFileDTO;
|
|
|
+import com.onemap.file.domain.ZhxzFileDTO;
|
|
|
+import com.onemap.file.mapper.ZhxzFileMapper;
|
|
|
+import com.onemap.file.utils.*;
|
|
|
+import com.onemap.system.api.domain.GhtgCl;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 本地文件存储
|
|
|
+ *
|
|
|
+ * @author onemap
|
|
|
+ */
|
|
|
+@Primary
|
|
|
+@Service
|
|
|
+public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
+ /**
|
|
|
+ * 资源映射路径 前缀
|
|
|
+ */
|
|
|
+ @Value("${file.prefix}")
|
|
|
+ public String localFilePrefix;
|
|
|
+ private TgclTypeDTO tgclTypeDTO = new TgclTypeDTO();
|
|
|
+ /**
|
|
|
+ * 智慧选址压缩包
|
|
|
+ */
|
|
|
+ @Resource
|
|
|
+ private ZhxzFileMapper zhxzFileMapper;
|
|
|
+ /**
|
|
|
+ * 域名或本机访问地址
|
|
|
+ */
|
|
|
+ @Value("${file.domain}")
|
|
|
+ public String domain;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件存储在本地的根路径
|
|
|
+ */
|
|
|
+ @Value("${file.path}")
|
|
|
+ private String localFilePath;
|
|
|
+ /**
|
|
|
+ * 专题图上传路径
|
|
|
+ */
|
|
|
+ @Value("${businessType.ztt}")
|
|
|
+ private String zttLocalFilePath;
|
|
|
+ /**
|
|
|
+ * 调规材料上传路径
|
|
|
+ */
|
|
|
+ @Value("${businessType.tgcl}")
|
|
|
+ private String tgclLocalFilePath;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * shp上传路径
|
|
|
+ */
|
|
|
+ @Value("${businessType.shp}")
|
|
|
+ private String shpLocalFilePath;
|
|
|
+ //文件上传后的转发路径配置
|
|
|
+ private Map<String, String> businessFilter = ImmutableMap.of("tgcl", "调规");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本地文件上传接口
|
|
|
+ *
|
|
|
+ * @param file 上传的文件
|
|
|
+ * @return 访问地址
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String uploadFile(MultipartFile file, String businessType) throws Exception {
|
|
|
+ String filepath = localFilePath;
|
|
|
+ if ("ztt".equals(businessType)) {
|
|
|
+ filepath = zttLocalFilePath;
|
|
|
+ } else if ("tgcl".equals(businessType)) {
|
|
|
+ filepath = tgclLocalFilePath;
|
|
|
+ }
|
|
|
+ String name = FileUploadUtils.upload(filepath, file);
|
|
|
+ String url = domain + localFilePrefix + name;
|
|
|
+ if (businessFilter.get(businessType) != null) {
|
|
|
+ url = domain + localFilePrefix + "/" + businessFilter.get(businessType) + "/" + name;
|
|
|
+ }
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分片上传
|
|
|
+ *
|
|
|
+ * @param file 文件
|
|
|
+ * @param chunk 块
|
|
|
+ * @param chunks 块
|
|
|
+ * @param md5 名字
|
|
|
+ * @return {@link ResponseEntity}<{@link String}>
|
|
|
+ * @throws IOException ioexception
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String splitFileUpload(@RequestParam("file") MultipartFile file,
|
|
|
+ @RequestParam("chunk") int chunk,
|
|
|
+ @RequestParam("chunks") int chunks,
|
|
|
+ @RequestParam("name") String name,
|
|
|
+ @RequestParam("md5") String md5) {
|
|
|
+ String folder = localFilePath + "\\" + md5 + "\\";
|
|
|
+ File dir = new File(folder);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ String path = folder + md5 + " _" + chunk + ".part";
|
|
|
+ try {
|
|
|
+ file.transferTo(new File(path));
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "文件保存失败!";
|
|
|
+ }
|
|
|
+ if (chunk == chunks - 1) {
|
|
|
+ String filepath = folder + name + "";
|
|
|
+ try {
|
|
|
+ FileUtils.mergeFile(folder, filepath);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "文件保存失败!";
|
|
|
+ }
|
|
|
+ // 删除分片文件
|
|
|
+ for (int i = 0; i < chunks; i++) {
|
|
|
+ try {
|
|
|
+ new File(folder + md5 + " _" + i + ".part").delete();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return filepath;
|
|
|
+ }
|
|
|
+ return "上传成功";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传附件类型文件(pdf, word, excel, jpeg, jpg, png...)
|
|
|
+ *
|
|
|
+ * @param file 文件
|
|
|
+ * @return 文件访问地址
|
|
|
+ * @throws Exception 抛出错误
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String uploadAppendix(MultipartFile file) throws Exception {
|
|
|
+ String filepath = localFilePath + File.separator + "att";
|
|
|
+ String name = FileUploadUtils.upload(filepath, file);
|
|
|
+ String url = domain + localFilePrefix + "/att" + name;
|
|
|
+
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本地word并转换pdf
|
|
|
+ *
|
|
|
+ * @param file 上传的文件
|
|
|
+ * @return 访问地址
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public WordToPdfDTO uploadWordFileToPdf(MultipartFile file, String businessType) throws Exception {
|
|
|
+ WordToPdfDTO pdf = new WordToPdfDTO();
|
|
|
+ String filepath = localFilePath;//9082打开
|
|
|
+ if ("tgcl".equals(businessType)) {
|
|
|
+ filepath = tgclLocalFilePath;
|
|
|
+ }
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
+ String name = FileUploadUtils.upload(filepath, file);
|
|
|
+ String kzmc = PublicTools.getFileKzmx(filename);
|
|
|
+ String pdfpath = "";
|
|
|
+ if (!kzmc.contains("pdf")) {
|
|
|
+ pdfpath = PdfUtils.file2pdf(filepath + name, name, kzmc);
|
|
|
+ } else {
|
|
|
+ pdfpath = name;
|
|
|
+ }
|
|
|
+ String url = domain + localFilePrefix + name;
|
|
|
+ if (businessFilter.get(businessType) != null) {
|
|
|
+ url = domain + localFilePrefix + "/" + businessFilter.get(businessType) + "/" + pdfpath;
|
|
|
+ }
|
|
|
+ pdf.setFilepath(filename);
|
|
|
+ pdf.setPdfpath(url);
|
|
|
+ pdf.setKzmc(kzmc);
|
|
|
+ return pdf;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public RequestResult uploadShpZip(MultipartFile file) {
|
|
|
+ //获得文件名称
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ //获得文件后缀名
|
|
|
+ String type = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
+ //判断上传的是否是压缩包
|
|
|
+ if (!type.equals("zip") && !type.equals("rar")) {
|
|
|
+ return RequestResult.error("请上传压缩包", null);
|
|
|
+ }
|
|
|
+ File dest = new File(shpLocalFilePath + "/" + fileName);
|
|
|
+ //获得上级目录
|
|
|
+ File dir = dest.getParentFile();
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ //获取当前时间的时间戳
|
|
|
+ long timeMillis = System.currentTimeMillis();
|
|
|
+ String shpPath = "";
|
|
|
+ try {
|
|
|
+ //上传文件
|
|
|
+ file.transferTo(dest);
|
|
|
+ //解压zip格式
|
|
|
+ if (type.equals("zip")) {
|
|
|
+ new UnPackageUtils().unPackZip(dest, shpLocalFilePath + "/" + timeMillis);
|
|
|
+ } else {
|
|
|
+ //解压rar格式
|
|
|
+ new UnPackageUtils().unPackRar(dest, shpLocalFilePath + "/" + timeMillis);
|
|
|
+ }
|
|
|
+ File[] files = new File(shpLocalFilePath + "/" + timeMillis).listFiles();
|
|
|
+ for (File file1 : files) {
|
|
|
+ if (file1.getAbsolutePath().substring(file1.getAbsolutePath().lastIndexOf(".") + 1).equals("shp")) {
|
|
|
+ shpPath = file1.getAbsolutePath();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!"".equals(shpPath)) {
|
|
|
+ ShpFileDTO res = new ShpFileDTO();
|
|
|
+ res.setFilepath(shpPath);
|
|
|
+ res.setGeojson(getShpGeoJson(shpPath));
|
|
|
+ res.setZippath(dest.getAbsolutePath());
|
|
|
+ return RequestResult.success("上传成功", res);
|
|
|
+ } else {
|
|
|
+ return RequestResult.error("未检索到shp文件");
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return RequestResult.error("上传失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public RequestResult uploadZhxzZip(MultipartFile file) {
|
|
|
+ //获得文件名称
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ //获得文件后缀名
|
|
|
+ String type = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
+ //判断上传的是否是压缩包
|
|
|
+ if (!type.equals("zip") && !type.equals("rar")) {
|
|
|
+ return RequestResult.error("请上传压缩包", null);
|
|
|
+ }
|
|
|
+ File dest = new File(shpLocalFilePath + "/" + fileName);
|
|
|
+ //获得上级目录
|
|
|
+ File dir = dest.getParentFile();
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ //获取当前时间的时间戳
|
|
|
+ long timeMillis = System.currentTimeMillis();
|
|
|
+ String shpPath = "";
|
|
|
+ try {
|
|
|
+ //上传文件
|
|
|
+ file.transferTo(dest);
|
|
|
+ //解压zip格式
|
|
|
+ if (type.equals("zip")) {
|
|
|
+ new UnPackageUtils().unPackZip(dest, shpLocalFilePath + "/" + timeMillis);
|
|
|
+ } else {
|
|
|
+ //解压rar格式
|
|
|
+ new UnPackageUtils().unPackRar(dest, shpLocalFilePath + "/" + timeMillis);
|
|
|
+ }
|
|
|
+ File[] files = new File(shpLocalFilePath + "/" + timeMillis).listFiles();
|
|
|
+ for (File file1 : files) {
|
|
|
+ if (file1.getAbsolutePath().substring(file1.getAbsolutePath().lastIndexOf(".") + 1).equals("shp")) {
|
|
|
+ shpPath = file1.getAbsolutePath();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!"".equals(shpPath)) {
|
|
|
+ String uuid = StringUtils.getUUID();
|
|
|
+ ZhxzFileDTO insertDto = new ZhxzFileDTO();
|
|
|
+ insertDto.setBsm(uuid);
|
|
|
+ insertDto.setWj(shpPath);
|
|
|
+ insertDto.setYswj(dest.getAbsolutePath());
|
|
|
+ zhxzFileMapper.insert(insertDto);
|
|
|
+ return RequestResult.success("上传成功", uuid);
|
|
|
+ } else {
|
|
|
+ return RequestResult.error("未检索到shp文件");
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return RequestResult.error("上传失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public GhtgCl uploadTgcl(MultipartFile file, Integer type) throws Exception {
|
|
|
+ GhtgCl cl = new GhtgCl();
|
|
|
+ cl.setBsm(StringUtils.getUUID());
|
|
|
+ cl.setWjdx((double) file.getSize());
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
+ cl.setFjmc(filename);
|
|
|
+ String kzmc = PublicTools.getFileKzmx(filename);
|
|
|
+ cl.setFjlx(type);
|
|
|
+ if (type == tgclTypeDTO.getShp()) {
|
|
|
+ RequestResult shpzip = uploadShpZip(file);
|
|
|
+ ShpFileDTO shpzipinfo = (ShpFileDTO) shpzip.get("data");
|
|
|
+ cl.setWjlj(shpzipinfo.getZippath());
|
|
|
+ cl.setYshp(shpzipinfo.getFilepath());
|
|
|
+ cl.setGeojson(shpzipinfo.getGeojson());
|
|
|
+ cl.setKzmc(kzmc);
|
|
|
+ cl.setYwj(shpzipinfo.getZippath());
|
|
|
+ return cl;
|
|
|
+ } else if (type == tgclTypeDTO.getTgcl()) {
|
|
|
+ WordToPdfDTO pdf = uploadWordFileToPdf(file, "tgcl");
|
|
|
+ cl.setWjlj(pdf.getPdfpath());
|
|
|
+ cl.setKzmc(pdf.getKzmc());
|
|
|
+ cl.setYwj(pdf.getFilepath());
|
|
|
+ return cl;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过shp路径转换esrijson并返回相应json文件路径
|
|
|
+ *
|
|
|
+ * @param path
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getShpEsriJsonPath(String path) {
|
|
|
+ String uuid = StringUtils.getUUID();
|
|
|
+ String publicpath = path.substring(0, path.indexOf("."));
|
|
|
+ String shppath = path;
|
|
|
+ String esrijsonpath = publicpath + "_" + uuid + "_" + "json.json";
|
|
|
+ String geojsonPath = publicpath + "_" + uuid + "_" + "geojson.json";
|
|
|
+ String geojson = ParsingShpFileUtils.shape2Geojson(shppath, geojsonPath);
|
|
|
+ String esrijson = EsriGeoJsonJsUtils.geo2ersi(geojson, "", shppath);
|
|
|
+ //写入文件
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ try {
|
|
|
+ fos = new FileOutputStream(esrijsonpath, false);
|
|
|
+ //true表示在文件末尾追加
|
|
|
+ fos.write(esrijson.toString().getBytes());
|
|
|
+ fos.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return esrijsonpath;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过shp路径转换geojson
|
|
|
+ *
|
|
|
+ * @param path
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String getShpGeoJson(String path) {
|
|
|
+ String uuid = StringUtils.getUUID();
|
|
|
+ String publicpath = path.substring(0, path.indexOf("."));
|
|
|
+ String shppath = path;
|
|
|
+ String geojsonPath = publicpath + "_" + uuid + "_" + "geojson.json";
|
|
|
+ String geojson = new ParsingShpFileUtils().shape2Geojson(shppath, geojsonPath);
|
|
|
+ return geojson;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public UploadZipDTO uploadZip(MultipartFile file) {
|
|
|
+ //获得文件名称
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ //获得文件后缀名
|
|
|
+ String type = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
+ String path = shpLocalFilePath + "/" + fileName;
|
|
|
+ File dest = new File(path);
|
|
|
+ //获得上级目录
|
|
|
+ File dir = dest.getParentFile();
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ //获取当前时间的时间戳
|
|
|
+ long timeMillis = System.currentTimeMillis();
|
|
|
+ String unzippath = shpLocalFilePath + "/" + timeMillis;
|
|
|
+ try {
|
|
|
+ //上传文件
|
|
|
+ file.transferTo(dest);
|
|
|
+ if (type.equals("zip")) {
|
|
|
+ new UnPackageUtils().unPackZip(dest, unzippath);
|
|
|
+ } else if (type.equals("rar")) {
|
|
|
+ //解压rar格式
|
|
|
+ new UnPackageUtils().unPackRar(dest, unzippath);
|
|
|
+ }
|
|
|
+ UploadZipDTO dto = new UploadZipDTO();
|
|
|
+ dto.setPath(path);
|
|
|
+ dto.setUnzippath(unzippath);
|
|
|
+ dto.setUuid(StringUtils.getUUID());
|
|
|
+ return dto;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|