|
@@ -0,0 +1,400 @@
|
|
|
+package com.fd.server.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.core.util.ZipUtil;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.fd.constant.Command;
|
|
|
+import com.fd.constant.MsgCode;
|
|
|
+import com.fd.constant.TypeCode;
|
|
|
+import com.fd.entity.FileEntity;
|
|
|
+import com.fd.entity.OutputFileEntity;
|
|
|
+import com.fd.repository.FileRepository;
|
|
|
+import com.fd.repository.OutputFileRepository;
|
|
|
+import com.fd.repository.UserRepository;
|
|
|
+import com.fd.server.ConvertServer;
|
|
|
+import com.fd.thread.AsyncTask;
|
|
|
+import com.fd.util.CmdUtil;
|
|
|
+import com.fd.util.FileUtils;
|
|
|
+import com.fd.util.R;
|
|
|
+import com.fd.util.RegexUtils;
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.concurrent.BlockingQueue;
|
|
|
+import java.util.concurrent.LinkedBlockingQueue;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by Owen on 2019/11/21 0021 15:29
|
|
|
+ */
|
|
|
+@Log4j2
|
|
|
+@Service
|
|
|
+public class ConvertServerImpl extends BaseServerImpl implements ConvertServer {
|
|
|
+
|
|
|
+ private String INPUT_PATH;
|
|
|
+
|
|
|
+ private String OUTPUT_PATH;
|
|
|
+
|
|
|
+
|
|
|
+ @Value("${base.path}")
|
|
|
+ private String BASE_PATH;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AsyncTask asyncTask;
|
|
|
+
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ private void init(){
|
|
|
+ this.INPUT_PATH = BASE_PATH + "/input/convert/";
|
|
|
+ this.OUTPUT_PATH = BASE_PATH + "/output/convert/";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FileRepository fileRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OutputFileRepository outputFileRepository;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserRepository userRepository;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 队列
|
|
|
+ */
|
|
|
+ private static BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(2);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保证线程安全的
|
|
|
+ */
|
|
|
+ private static AtomicInteger count = new AtomicInteger();
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R uploads(MultipartFile[] files, HttpServletRequest req, String type, String coord) {
|
|
|
+ log.warn("run uploads");
|
|
|
+ if (files != null && files.length > 0) {
|
|
|
+
|
|
|
+ // 创建目录
|
|
|
+ String time = DateUtil.format(new DateTime(), "yyyyMMdd_HHmmss");
|
|
|
+ String filePath = INPUT_PATH + time;
|
|
|
+ FileUtils.createDir(filePath);
|
|
|
+ log.info("filePath: {}", filePath);
|
|
|
+
|
|
|
+ // 存储多个文件名
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+
|
|
|
+ // 数据开关
|
|
|
+ boolean flag = true;
|
|
|
+
|
|
|
+ // 检查文件名、格式
|
|
|
+ int suffixCheckInt = 0;
|
|
|
+ HashSet<Object> setPrefix = new HashSet<>();
|
|
|
+
|
|
|
+ // 记录shp 信息
|
|
|
+ boolean shpFlag = false;
|
|
|
+ String shpName = "";
|
|
|
+ String filename = "";
|
|
|
+
|
|
|
+ for (MultipartFile f : files) {
|
|
|
+ filename = f.getOriginalFilename();
|
|
|
+
|
|
|
+ // 文件是否包含中文字符
|
|
|
+ if (RegexUtils.regexChinese(filename)) {
|
|
|
+ return new R(51005, MsgCode.E51005);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (TypeCode.FILE_TYPE_VECTOR.equals(type)) {
|
|
|
+ log.info("fileName : {}", filename);
|
|
|
+ String s = StringUtils.substringAfterLast(filename, ".");
|
|
|
+ // 判断后缀名,需要包含这以下四个
|
|
|
+ String [] suffixCheck = {"dbf","shp","shx","prj"};
|
|
|
+ if (Arrays.asList(suffixCheck).contains(s)) {
|
|
|
+ suffixCheckInt += 1;
|
|
|
+
|
|
|
+ // 判断前缀名字是否一致
|
|
|
+ String prefix = StringUtils.substringBeforeLast(filename, ".");
|
|
|
+ setPrefix.add(prefix);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if ("shp".equals(s)) {
|
|
|
+ shpFlag = true;
|
|
|
+ shpName = filename;
|
|
|
+ }
|
|
|
+
|
|
|
+ sb.append(filename).append(",");
|
|
|
+
|
|
|
+ } else {
|
|
|
+ String s = StringUtils.substringAfterLast(filename, ".");
|
|
|
+ if (!"tif".equals(s)) {
|
|
|
+ return new R(50008,MsgCode.E50008);
|
|
|
+ }
|
|
|
+ sb.append(filename);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+ FileUtils.bigFileWrite(f.getInputStream(), filePath + "/" + filename);
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ flag = false;
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.warn("FileWrite success");
|
|
|
+
|
|
|
+
|
|
|
+ if (TypeCode.FILE_TYPE_VECTOR.equals(type)) {
|
|
|
+ // 上传文件名是否一致
|
|
|
+ if (setPrefix.size() > 1) {
|
|
|
+
|
|
|
+ log.info("文件名前缀不一致");
|
|
|
+ return new R(51009, MsgCode.E51009);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上传文件格式有误
|
|
|
+ if (suffixCheckInt != 4) {
|
|
|
+ log.info("文件后缀名有误");
|
|
|
+ return new R(51008, MsgCode.E51008);
|
|
|
+ }
|
|
|
+
|
|
|
+ filePath = filePath + "/" + shpName;
|
|
|
+ filename = shpName;
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ filePath = filePath + "/" + filename;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 保存信息
|
|
|
+ OutputFileEntity outputFile = new OutputFileEntity();
|
|
|
+ if (flag) {
|
|
|
+ FileEntity entity = new FileEntity();
|
|
|
+ entity.setDirectory(time);
|
|
|
+ entity.setFileName(sb.toString());
|
|
|
+ // 只存目录,没有存完整路径
|
|
|
+ entity.setFileUrl(filePath);
|
|
|
+ entity.setCreateTime(new Date());
|
|
|
+ entity.setUpdateTime(new Date());
|
|
|
+ entity.setType(TypeCode.FILE_TYPE_CONVERT);
|
|
|
+ entity.setCoord(coord);
|
|
|
+ entity.setResStatus(0);
|
|
|
+ entity = fileRepository.save(entity);
|
|
|
+
|
|
|
+ outputFile.setUploadId(entity.getId());
|
|
|
+ outputFile.setUploadPath(entity.getFileUrl());
|
|
|
+ outputFile.setDirectory(entity.getDirectory());
|
|
|
+ outputFile.setFileName(filename);
|
|
|
+ // 2:未转坐标
|
|
|
+ outputFile.setStatus(2);
|
|
|
+ outputFile.setType(entity.getType());
|
|
|
+ outputFile.setCoord(entity.getCoord());
|
|
|
+ outputFile.setCreateTime(new Date());
|
|
|
+ outputFile.setUpdateTime(new Date());
|
|
|
+ outputFile.setResStatus(0);
|
|
|
+ outputFile.setFileType(type);
|
|
|
+
|
|
|
+ outputFile = outputFileRepository.save(outputFile);
|
|
|
+ log.warn("end uploads");
|
|
|
+ return new R(200, outputFile);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 栅格、矢量数据坐标转换
|
|
|
+ * @param fileId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R cmdTransformCoord(Long fileId) {
|
|
|
+ log.warn("run cmdTransformCoord id: {}", fileId);
|
|
|
+ OutputFileEntity entity = getOutputFileEntity(fileId);
|
|
|
+ if (entity == null) {
|
|
|
+ return new R(50002, MsgCode.E50002);
|
|
|
+ }
|
|
|
+
|
|
|
+ String cmdStep1 = null;
|
|
|
+ String cmdStep2 = null;
|
|
|
+ if (TypeCode.FILE_TYPE_RASTER.equals(entity.getFileType())) {
|
|
|
+ cmdStep1 = Command.convert_TRANSFORM_coord_raster_step1;
|
|
|
+ cmdStep2 = Command.convert_TRANSFORM_coord_raster_step2;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ cmdStep1 = Command.convert_TRANSFORM_coord_vector_step1;
|
|
|
+ cmdStep2 = Command.convert_TRANSFORM_coord_vector_step2;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ String step1Path = OUTPUT_PATH + entity.getDirectory() +"/step1/";
|
|
|
+ String step2Path = OUTPUT_PATH + entity.getDirectory() +"/step2/";
|
|
|
+ FileUtils.createDir(step1Path);
|
|
|
+ FileUtils.createDir(step2Path);
|
|
|
+
|
|
|
+ cmdStep1 = cmdStep1.replace("@coord", entity.getCoord());
|
|
|
+ cmdStep1 = cmdStep1.replace("@inputFile", entity.getUploadPath());
|
|
|
+ cmdStep1 = cmdStep1.replace("@outputFile", step1Path + entity.getFileName());
|
|
|
+
|
|
|
+ cmdStep2 = cmdStep2.replace("@inputFile", step1Path + entity.getFileName());
|
|
|
+ cmdStep2 = cmdStep2.replace("@outputFile", step2Path + entity.getFileName());
|
|
|
+
|
|
|
+ log.info("cmdStep1: {}", cmdStep1);
|
|
|
+ Integer step1Flag = CmdUtil.exeCmdSingle(cmdStep1);
|
|
|
+
|
|
|
+ Integer step2Flag = 1;
|
|
|
+
|
|
|
+ if (step1Flag == 0) {
|
|
|
+ log.info("cmdStep2: {}", cmdStep2);
|
|
|
+ step2Flag = CmdUtil.exeCmdSingle(cmdStep2);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (step2Flag == 0){
|
|
|
+ entity.setUpdateTime(new Date());
|
|
|
+ entity.setCoordGeneralPath(step1Path + entity.getFileName());
|
|
|
+ entity.setCoordStrictPath(step2Path + entity.getFileName());
|
|
|
+ // 完成
|
|
|
+ entity.setStatus(13);
|
|
|
+ entity = outputFileRepository.save(entity);
|
|
|
+ log.warn("end cmdTransformCoord id: {}", fileId);
|
|
|
+ return new R(200, entity);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return new R(50005, MsgCode.E50005);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void download(Long fileId, HttpServletResponse response) {
|
|
|
+ log.warn("run download");
|
|
|
+
|
|
|
+ OutputFileEntity entity = getOutputFileEntity(fileId);
|
|
|
+ if (entity != null) {
|
|
|
+ log.warn("run fileType: {}", entity.getFileType());
|
|
|
+ if (TypeCode.FILE_TYPE_RASTER.equals(entity.getFileType())) {
|
|
|
+ try {
|
|
|
+ log.warn("fileDownload: {}", entity.getCoordStrictPath());
|
|
|
+ FileUtils.fileDownload(response, entity.getCoordStrictPath());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ String filePath = entity.getCoordStrictPath();
|
|
|
+ // 得到目录
|
|
|
+ filePath = StringUtils.substringBeforeLast(filePath, "/");
|
|
|
+ log.info("filePath: {}", filePath);
|
|
|
+
|
|
|
+ String outputZip = OUTPUT_PATH + entity.getDirectory();
|
|
|
+
|
|
|
+ String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
|
|
|
+ // 压缩文件
|
|
|
+ outputZip = outputZip + File.separator + fileName + ".zip";
|
|
|
+ log.info("outputZip: {}", filePath);
|
|
|
+ ZipUtil.zip(filePath, outputZip);
|
|
|
+
|
|
|
+ // 下载文件
|
|
|
+ try {
|
|
|
+ log.warn("fileDownload: {}", outputZip);
|
|
|
+ FileUtils.fileDownload(response, outputZip);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.warn("end download");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String filePath = "F:\\test\\test123\\aa.txt";
|
|
|
+ String inPath = "F:/test/test123";
|
|
|
+ String inPath1 = "F:\\test\\test123\\";
|
|
|
+ String outPath = "F:\\test\\test1234\\1.zip";
|
|
|
+
|
|
|
+ String s = StringUtils.substringBefore("AA.JPG", ".");
|
|
|
+
|
|
|
+ // 得到目录
|
|
|
+// filePath = StringUtils.substringBeforeLast(filePath, File.separator);
|
|
|
+ System.out.println(s);
|
|
|
+// ZipUtil.zip(inPath1, outPath, true);
|
|
|
+
|
|
|
+// ZipUtil.zip("f:/test/test123", "f:/test/1234/2.zip");
|
|
|
+// byte[] gzip = ZipUtil.gzip(new File("f:/test/1234/2.zip"));
|
|
|
+// HttpUtil.download()
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重写 deleteById
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R deleteById(Long id) {
|
|
|
+ log.info("run deleteById: {}", id);
|
|
|
+ OutputFileEntity entity = getOutputFileEntity(id);
|
|
|
+
|
|
|
+ if (entity == null) {
|
|
|
+ return new R(50002, MsgCode.E50002);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除文件
|
|
|
+ FileUtils.delFolder(INPUT_PATH + entity.getDirectory());
|
|
|
+ FileUtils.delFolder(OUTPUT_PATH + entity.getDirectory());
|
|
|
+
|
|
|
+ // 删除db数据
|
|
|
+ fileRepository.deleteById(entity.getUploadId());
|
|
|
+ outputFileRepository.deleteById(id);
|
|
|
+
|
|
|
+
|
|
|
+ log.info("end deleteById: {}", id);
|
|
|
+ return new R(200, MsgCode.SUCCESS);
|
|
|
+ }
|
|
|
+
|
|
|
+ private OutputFileEntity getOutputFileEntity(Long id) {
|
|
|
+ Optional<OutputFileEntity> o = outputFileRepository.findById(id);
|
|
|
+ if (o.isPresent()) {
|
|
|
+ return o.get();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|