瀏覽代碼

添加dam2obj功能, 测试已通过

wuweihao 2 年之前
父節點
當前提交
a4fcad3590

+ 3 - 2
gis_application/src/main/resources/application-dev.properties

@@ -52,12 +52,13 @@ spring.redis.jedis.pool.max-wait=-1ms
 
 
 #log
-logging.file.path=E:/javaProject/${project.en}_log
+logging.file.path=E:/log/${project.en}_log
 logging.config=classpath:logback-spring.xml
 logging.level.com.gis=debug
 
 # file info
-server.file.path=F:\\test\\ngin\\${project.en}_data\\
+#server.file.path=F:\\test\\ngin\\${project.en}_data\\
+server.file.path=D:\\data\\${project.en}_data\\
 server.domain=192.168.0.135:${server.port}
 
 # \u76EE\u524D\u4E0D\u7528\u540E\u7F00

+ 101 - 0
gis_common/src/main/java/com/gis/common/util/DateUtils.java

@@ -0,0 +1,101 @@
+package com.gis.common.util;
+
+import cn.hutool.core.date.DateUtil;
+import org.junit.Test;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
+
+/**
+ * Created by owen on 2022/4/8 0008 14:32
+ */
+public class DateUtils extends DateUtil {
+
+    private static String YYYY_MM = "yyyy-MM";
+
+    private static String YYYYMM = "yyyyMM";
+
+    private static String YYYY_MM_DD = "yyyy-MM-dd";
+
+    private static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
+
+
+    private static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
+
+    private static String YYYYMMDD_HHMMSSSSS = "yyyyMMdd_HHmmssSSS";
+
+    private static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
+
+
+    /**
+     * 获取当前月
+     * @return
+     */
+    public static String getMonth(){
+        return format(LocalDateTime.now(), YYYYMM);
+    }
+
+    /**
+     * 获取当前月份的前几个月
+     * @param month
+     * @return
+     */
+    public static String minusMonths(int month){
+        return format(LocalDateTime.now().minusMonths(month), YYYYMM);
+    }
+
+    /**
+     * 获取当前时间戳
+     * @return
+     */
+    public static String getDateTime(){
+        return format(LocalDateTime.now(), YYYYMMDD_HHMMSSSSS);
+    }
+
+
+    /**
+     * 字符串转LocalDateTime
+     * @param time 字符串
+     * @return LocalDateTime
+     */
+    public static LocalDateTime srtToLocalDateTime(String time){
+        return LocalDateTime.parse(time, DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM));
+    }
+
+    public static LocalDate srtToLocalDate(String time){
+        return LocalDate.parse(time, DateTimeFormatter.ofPattern(YYYY_MM_DD));
+    }
+
+
+
+    /**
+     * 是否小于当前时间
+     * @param date
+     * @return
+     */
+    public static boolean isBefore(LocalDate date){
+        LocalDateTime localDateTime = date.atTime(LocalTime.of(00,00, 00));
+        return localDateTime.isBefore(LocalDateTime.now());
+
+    }
+
+    /**
+     *
+     * @param date yyyy-MM-dd
+     * @return
+     */
+    public static boolean isBefore(String date){
+        LocalDate localDate = srtToLocalDate(date);
+        return isBefore(localDate);
+
+    }
+
+
+
+    @Test
+    public void test(){
+        System.out.println(srtToLocalDateTime("2022-02-16 12:26"));
+    }
+}

+ 74 - 1
gis_common/src/main/java/com/gis/common/util/FileUtils.java

@@ -4,11 +4,15 @@ import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.img.ImgUtil;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.lang.Validator;
+import cn.hutool.core.util.StrUtil;
 import cn.hutool.core.util.URLUtil;
+import com.gis.common.constant.ConfigConstant;
 import com.gis.common.exception.BaseRuntimeException;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
 import org.springframework.util.ResourceUtils;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -17,13 +21,20 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * Created by owen on 2020/5/12 0012 17:21
  */
 @Slf4j
+@Component
 public class FileUtils {
 
+    @Autowired
+    ConfigConstant configConstant;
+
+    // 确保同一时间上传文件的唯一性
+    private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();
 
     /**
      * 多文件上传
@@ -65,6 +76,68 @@ public class FileUtils {
 
     }
 
+    /**
+     *
+     * @param file
+     * @param isPinYinRename false:时间戳重命名, true:用拼音重名文件
+     * @param savePath 保存地址(前面有斜杠, 后面没有),没有文件名
+     * @return 文件名
+     */
+    public String upload(MultipartFile file, String savePath, boolean isPinYinRename) {
+
+        // 检查非法文件上传
+        boolean checkFile = this.checkFile(file);
+        if (!checkFile) {
+            throw new BaseRuntimeException("上传文件格式有误, 请重新上传");
+        }
+
+        // 文件目录
+        String fileName = file.getOriginalFilename();
+        String newName;
+        if (isPinYinRename){
+            newName = RegexUtil.getPinyinName(fileName);
+        } else {
+            String suffix = StrUtil.subAfter(fileName, ".", true);
+            newName =  DateUtils.getDateTime() + ATOMIC_INTEGER.incrementAndGet() + "." + suffix;
+        }
+
+        savePath = configConstant.serverBasePath + savePath + "/" + newName;
+        log.info("保存文件地址:{}", savePath);
+
+        try {
+            FileUtil.writeFromStream(file.getInputStream(), savePath);
+
+            return newName;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+
+    /**
+     *
+     * @param file
+     * @param isPinYinRename false:时间戳重命名, true:用拼音重名文件
+     * @param savePath 保存地址(前面有斜杠, 后面没有),没有文件名
+     * @return map
+     */
+    public Map<String, Object> uploadMap(MultipartFile file, String savePath, boolean isPinYinRename) {
+        String newName = this.upload(file, savePath, isPinYinRename);
+        HashMap<String, Object> result = new HashMap<>();
+        // 防止名字过长,超过数据库长度
+        String originalFilename = file.getOriginalFilename();
+        if (originalFilename != null && originalFilename.length() > 64){
+            String suffix = StrUtil.subAfter(originalFilename, ".", true);
+            originalFilename = originalFilename.substring(64);
+            originalFilename = originalFilename + "." + suffix;
+        }
+        result.put("fileName", originalFilename);
+        result.put("filePath", savePath + "/" + newName);
+        return result;
+
+    }
+
 
     /**
      * 单文件上传
@@ -210,7 +283,7 @@ public class FileUtils {
 
     public static boolean checkFile(MultipartFile file) {
         //设置允许上传文件类型
-        String suffixList = ".jpg,.gif,.png,.ico,.bmp,.jpeg,.zip,.zp,.rar,.mp3,.mp4,.avi,.mov,.4dage";
+        String suffixList = ".jpg,.gif,.png,.ico,.bmp,.jpeg,.zip,.zp,.rar,.mp3,.mp4,.avi,.mov,.4dage,.dam";
         // 获取文件后缀
         if(file == null){
             log.info("文件流为空不可上传");

+ 6 - 0
gis_common/src/main/java/com/gis/common/util/RandomUtils.java

@@ -14,6 +14,12 @@ public class RandomUtils {
         return RandomUtil.randomString(baseString, length);
     }
 
+    public static String getCode(String prefix){
+        String s = randowString(9);
+
+        return prefix + "_" + s;
+    }
+
     public static void main(String[] args) {
         System.out.println(randowString(9));
     }

+ 25 - 0
gis_domain/src/main/java/com/gis/domain/dto/FileDto.java

@@ -0,0 +1,25 @@
+package com.gis.domain.dto;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * Created by owen on 2022/5/19 0019 15:40
+ * 内容表
+ *
+ */
+@Data
+public class FileDto {
+
+//    @NotBlank(message = "类型不能为空")
+//    @ApiModelProperty(value = "类型:img|model", required = true)
+//    private String type;
+
+    @ApiModelProperty(value = "文件")
+    private MultipartFile file;
+
+
+
+
+}

+ 12 - 0
gis_service/src/main/java/com/gis/service/OpsService.java

@@ -0,0 +1,12 @@
+package com.gis.service;
+
+import com.gis.common.util.Result;
+import com.gis.domain.dto.FileDto;
+
+/**
+ * Created by owen on 2022/9/13 0013 15:16
+ */
+public interface OpsService {
+
+    Result damToObj(FileDto param);
+}

+ 96 - 0
gis_service/src/main/java/com/gis/service/impl/OpsServiceImpl.java

@@ -0,0 +1,96 @@
+package com.gis.service.impl;
+
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.util.RandomUtil;
+import com.gis.common.constant.ConfigConstant;
+import com.gis.common.exception.BaseRuntimeException;
+import com.gis.common.proto.util.ConvertUtils;
+import com.gis.common.util.CmdUtils;
+import com.gis.common.util.FileUtils;
+import com.gis.common.util.RandomUtils;
+import com.gis.common.util.Result;
+import com.gis.domain.dto.FileDto;
+import com.gis.service.OpsService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.util.Map;
+
+/**
+ * Created by owen on 2022/9/13 0013 15:16
+ */
+@Slf4j
+@Service
+public class OpsServiceImpl implements OpsService {
+
+    @Autowired
+    ConfigConstant configConstant;
+
+    @Autowired
+    FileUtils fileUtils;
+
+    @Override
+    public Result damToObj(FileDto param) {
+
+        // 创建测试场景码
+        String code = RandomUtils.getCode("t");
+        log.info("场景码: {}", code);
+
+        String basePath = configConstant.serverBasePath;
+
+        // 写入文件
+        Map<String, Object> uploadMap = fileUtils.uploadMap(param.getFile(), "/" + code, true);
+        Object filePath = uploadMap.get("filePath");
+        String damPath = basePath + filePath;
+
+        // dam2txt
+        doDam2Txt(damPath, code);
+
+        // txt2obj
+        doTxt2Obj(code);
+
+
+        return Result.success(basePath + code);
+    }
+
+
+    private void doDam2Txt(String damPath, String code){
+
+        // dam2txt
+        String testModelDataTxtPath = configConstant.serverBasePath + code + "/test.txt";
+        if (FileUtil.exist(testModelDataTxtPath)) {
+            FileUtil.del(testModelDataTxtPath);
+        }
+        try {
+            ConvertUtils.convertDamToTxt2(damPath, testModelDataTxtPath);
+            log.info("dam2Txt完成, path: {}", testModelDataTxtPath);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+    }
+
+    private void doTxt2Obj(String code){
+        // todo modeldata.txt to mesh.obj
+        String basePath = configConstant.serverBasePath + code;
+//        String txt2objDir = basePath+ "/test.txt";
+        String txt2objDir = configConstant.serverBasePath + "tools/txt2obj";
+        String txt2objCmd = "python " + configConstant.serverBasePath + "tools\\txt2obj\\run.py " + txt2objDir;
+        CmdUtils.callLine(txt2objCmd);
+        String meshObjPath = txt2objDir + "/test.obj";
+        String meshObjMtlPath = txt2objDir + "/test.mtl";
+        if (!FileUtil.exist(meshObjPath)) {
+            throw new BaseRuntimeException("算法生成test.obj失败: " + meshObjPath);
+        }
+        log.info("mesh.obj生成完成");
+
+        // 将mesh.obj、mesh.obj.mtl复制到场景码跟目录
+        String objPath = basePath + "/test.obj";
+        String mntPath = basePath + "/test.mtl";
+        FileUtil.copy(new File(meshObjPath), new File(objPath), true);
+        FileUtil.copy(new File(meshObjMtlPath), new File(mntPath), true);
+        log.info("test.obj复制到根目录完成: {}", basePath);
+    }
+}

+ 30 - 0
gis_web/src/main/java/com/gis/web/controller/OpsController.java

@@ -0,0 +1,30 @@
+package com.gis.web.controller;
+
+import com.gis.common.util.Result;
+import com.gis.domain.dto.FileDto;
+import com.gis.service.OpsService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * Created by owen on 2022/9/13 0013 15:15
+ * 测试已通过
+ */
+@Api(tags = "ops-项目运维-免token")
+@RestController
+@RequestMapping("/api/ops")
+public class OpsController {
+
+    @Autowired
+    OpsService entityService;
+
+    @ApiOperation(value = "dam转obj", notes = "")
+    @PostMapping("/dam2obj")
+    public Result damToObj(FileDto param){
+        return entityService.damToObj(param);
+    }
+}