Browse Source

文件转换api

dengsixing 3 năm trước cách đây
mục cha
commit
3531036b3f

+ 21 - 44
4dkankan-center-scene/src/main/java/com/fdkankan/scene/controller/FileConvertController.java

@@ -4,17 +4,21 @@ import cn.hutool.core.io.FileTypeUtil;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.lang.UUID;
+import com.fdkankan.common.constant.ConstantFileName;
 import com.fdkankan.common.constant.ConstantFilePath;
 import com.fdkankan.common.constant.ErrorCode;
 import com.fdkankan.common.controller.BaseController;
 import com.fdkankan.common.exception.BusinessException;
 import com.fdkankan.common.util.CreateObjUtil;
+import com.fdkankan.scene.service.IFileConvertService;
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.nio.file.Paths;
 import javax.servlet.ServletOutputStream;
+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.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -33,55 +37,28 @@ import org.springframework.web.multipart.MultipartFile;
 @RequestMapping("/service/file/convert")
 public class FileConvertController extends BaseController {
 
-    @RequestMapping(value = "/getModeldata", method = RequestMethod.POST)
-    public void convertTxtToModeldata(@RequestParam("file") MultipartFile file) throws Exception{
-
-        //生成uuid
-        String fileName = "vision.modeldata";
-        String uuid = UUID.randomUUID().toString();
-
-        String path = ConstantFilePath.FILE_CONVERT_PATH + uuid;
-//        String path = "F:\\mnt\\4Dkankan\\fileConvert\\" + uuid;
-        String srcPath = path + "/" + file.getOriginalFilename();
-        String targetPath = path + "/vision.modeldata";
-        File srcFile = new File(srcPath);
-        if(!srcFile.getParentFile().exists()){
-            srcFile.getParentFile().mkdirs();
-        }
-        file.transferTo(new File(srcPath));
-        String type = FileTypeUtil.getType(srcFile);
-        if(!"txt".equals(type)){
-            throw new BusinessException(ErrorCode.FAILURE_CODE_7007, "txt");
-        }
-        CreateObjUtil.convertTxtToVisionmodeldata(srcPath, targetPath);
-        if(!FileUtil.exist(targetPath)){
-            throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
-        }
-
-        // 设置返回内容格式
-        response.setContentType("application/octet-stream");
-
-        // 把文件名按UTF-8取出并按ISO8859-1编码,保证弹出窗口中的文件名中文不乱码
-        // 中文不要太多,最多支持17个中文,因为header有150个字节限制。
-        // 这一步一定要在读取文件之后进行,否则文件名会乱码,找不到文件
-//        fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
+    @Autowired
+    private IFileConvertService fileConvertService;
 
-        // 设置下载弹窗的文件名和格式(文件名要包括名字和文件格式)
-        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
-
-        try(
-            BufferedInputStream inputStream = FileUtil.getInputStream(targetPath);
-            ServletOutputStream outputStream = response.getOutputStream();
-            ){
-            IoUtil.copy(inputStream, outputStream);
-            //关闭流
-            IoUtil.close(outputStream);
-            IoUtil.close(inputStream);
-        }
+    @PostMapping("/getModeldata")
+    public void convertTxtToModeldata(@RequestParam("file") MultipartFile file) throws Exception{
+        fileConvertService.convertTxtToModeldata(file, this.response);
+    }
 
+    @PostMapping("/getDam")
+    public void convertTxtToDam(@RequestParam("file") MultipartFile file) throws Exception{
+        fileConvertService.convertTxtToDam(file, this.response);
+    }
 
+    @PostMapping("/getLzma")
+    public void convertTxtToLzma(@RequestParam("file") MultipartFile file) throws Exception{
+        fileConvertService.convertTxtToLzma(file, this.response);
     }
 
+//    CreateObjUtil.convertTxtToDam( path + File.separator + "results" + File.separator+"tex"+File.separator+"modeldata.txt", path + File.separator + "results" +File.separator+ ConstantFileName.modelUUID+"_50k.dam");
+//        CreateObjUtil.convertDamToLzma(path + File.separator + "results");
+//        CreateObjUtil.convertTxtToDam( path + File.separator + "results" +File.separator+"tex"+File.separator+"modeldata.txt", path + File.separator + "results" + File.separator+ConstantFileName.modelUUID+"_50k.dam");
+
 
 
 

+ 25 - 0
4dkankan-center-scene/src/main/java/com/fdkankan/scene/service/IFileConvertService.java

@@ -0,0 +1,25 @@
+package com.fdkankan.scene.service;
+
+import javax.servlet.http.HttpServletResponse;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * <p>
+ * TODO
+ * </p>
+ *
+ * @author dengsixing
+ * @since 2022/5/17
+ **/
+public interface IFileConvertService {
+
+    void convertTxtToModeldata(MultipartFile file, HttpServletResponse response) throws Exception;
+
+    void convertTxtToDam(MultipartFile file, HttpServletResponse response) throws Exception;
+
+    void convertTxtToLzma(MultipartFile file, HttpServletResponse response) throws Exception;
+
+
+
+}

+ 185 - 0
4dkankan-center-scene/src/main/java/com/fdkankan/scene/service/impl/FileConvertServiceImpl.java

@@ -0,0 +1,185 @@
+package com.fdkankan.scene.service.impl;
+
+import cn.hutool.core.io.FileTypeUtil;
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.lang.UUID;
+import com.fdkankan.common.constant.ConstantFileName;
+import com.fdkankan.common.constant.ConstantFilePath;
+import com.fdkankan.common.constant.ErrorCode;
+import com.fdkankan.common.exception.BusinessException;
+import com.fdkankan.common.util.CreateObjUtil;
+import com.fdkankan.scene.service.IFileConvertService;
+import java.io.BufferedInputStream;
+import java.io.File;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * <p>
+ * TODO
+ * </p>
+ *
+ * @author dengsixing
+ * @since 2022/5/17
+ **/
+@Slf4j
+@Service
+public class FileConvertServiceImpl implements IFileConvertService {
+
+    @Override
+    public void convertTxtToModeldata(MultipartFile file, HttpServletResponse response) throws Exception {
+
+        //生成uuid
+        String fileName = "vision.modeldata";
+        String uuid = UUID.randomUUID().toString();
+        String path = ConstantFilePath.FILE_CONVERT_PATH + uuid;
+//        String path = "F:\\mnt\\4Dkankan\\fileConvert\\" + uuid;
+        String srcPath = path + File.separator + file.getOriginalFilename();
+        String targetPath = path + File.separator + fileName;
+        File srcFile = new File(srcPath);
+        if(!srcFile.getParentFile().exists()){
+            srcFile.getParentFile().mkdirs();
+        }
+        file.transferTo(new File(srcPath));
+        String type = FileTypeUtil.getType(srcFile);
+        if(!"txt".equals(type)){
+            throw new BusinessException(ErrorCode.FAILURE_CODE_7007, "txt");
+        }
+        CreateObjUtil.convertTxtToVisionmodeldata(srcPath, targetPath);
+        if(!FileUtil.exist(targetPath)){
+            throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
+        }
+
+        // 设置返回内容格式
+        response.setContentType("application/octet-stream");
+
+        // 把文件名按UTF-8取出并按ISO8859-1编码,保证弹出窗口中的文件名中文不乱码
+        // 中文不要太多,最多支持17个中文,因为header有150个字节限制。
+        // 这一步一定要在读取文件之后进行,否则文件名会乱码,找不到文件
+//        fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
+
+        // 设置下载弹窗的文件名和格式(文件名要包括名字和文件格式)
+        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
+
+        try(
+            BufferedInputStream inputStream = FileUtil.getInputStream(targetPath);
+            ServletOutputStream outputStream = response.getOutputStream();
+        ){
+            IoUtil.copy(inputStream, outputStream);
+            //关闭流
+            IoUtil.close(outputStream);
+            IoUtil.close(inputStream);
+        }catch (Exception e){
+            log.error("文件流输出失败,文件路径: " + targetPath, e);
+            throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
+        }
+    }
+
+    @Override
+    public void convertTxtToDam(MultipartFile file, HttpServletResponse response) throws Exception {
+        //生成uuid
+        String fileName = ConstantFileName.modelUUID+"_50k.dam";
+        String uuid = UUID.randomUUID().toString();
+        String path = ConstantFilePath.FILE_CONVERT_PATH + uuid;
+//        String path = "F:\\mnt\\4Dkankan\\fileConvert\\" + uuid;
+        String srcPath = path + File.separator + file.getOriginalFilename();
+        String targetPath = path + File.separator + fileName;
+        File srcFile = new File(srcPath);
+        if(!srcFile.getParentFile().exists()){
+            srcFile.getParentFile().mkdirs();
+        }
+        file.transferTo(new File(srcPath));
+        String type = FileTypeUtil.getType(srcFile);
+        if(!"txt".equals(type)){
+            throw new BusinessException(ErrorCode.FAILURE_CODE_7007, "txt");
+        }
+        CreateObjUtil.convertTxtToDam( srcPath, targetPath);
+        if(!FileUtil.exist(targetPath)){
+            throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
+        }
+
+        // 设置返回内容格式
+        response.setContentType("application/octet-stream");
+
+        // 把文件名按UTF-8取出并按ISO8859-1编码,保证弹出窗口中的文件名中文不乱码
+        // 中文不要太多,最多支持17个中文,因为header有150个字节限制。
+        // 这一步一定要在读取文件之后进行,否则文件名会乱码,找不到文件
+//        fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
+
+        // 设置下载弹窗的文件名和格式(文件名要包括名字和文件格式)
+        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
+
+        try(
+            BufferedInputStream inputStream = FileUtil.getInputStream(targetPath);
+            ServletOutputStream outputStream = response.getOutputStream();
+        ){
+            IoUtil.copy(inputStream, outputStream);
+            //关闭流
+            IoUtil.close(outputStream);
+            IoUtil.close(inputStream);
+        }catch (Exception e){
+            log.error("文件流输出失败,文件路径: " + targetPath, e);
+            throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
+        }
+    }
+
+    @Override
+    public void convertTxtToLzma(MultipartFile file, HttpServletResponse response) throws Exception {
+
+        //生成uuid
+        String damFileName = ConstantFileName.modelUUID+"_50k.dam";
+        String lzmaFileName = damFileName + ".lzma";
+        String uuid = UUID.randomUUID().toString();
+        String path = ConstantFilePath.FILE_CONVERT_PATH + uuid;
+//        String path = "F:\\mnt\\4Dkankan\\fileConvert\\" + uuid;
+        String srcPath = path + File.separator + file.getOriginalFilename();
+        String damFilePath = path + File.separator + damFileName;
+        String lzmaFilePath = path + File.separator + lzmaFileName;
+        File srcFile = new File(srcPath);
+        if(!srcFile.getParentFile().exists()){
+            srcFile.getParentFile().mkdirs();
+        }
+        file.transferTo(new File(srcPath));
+        String type = FileTypeUtil.getType(srcFile);
+        if(!"txt".equals(type)){
+            throw new BusinessException(ErrorCode.FAILURE_CODE_7007, "txt");
+        }
+        CreateObjUtil.convertTxtToDam( srcPath, damFilePath);
+        if(!FileUtil.exist(damFilePath)){
+            throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
+        }
+        CreateObjUtil.convertDamToLzmaByAbsolutePath(damFilePath);
+        if(!FileUtil.exist(lzmaFilePath)){
+            throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
+        }
+
+        // 设置返回内容格式
+        response.setContentType("application/octet-stream");
+
+        // 把文件名按UTF-8取出并按ISO8859-1编码,保证弹出窗口中的文件名中文不乱码
+        // 中文不要太多,最多支持17个中文,因为header有150个字节限制。
+        // 这一步一定要在读取文件之后进行,否则文件名会乱码,找不到文件
+//        fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
+
+        // 设置下载弹窗的文件名和格式(文件名要包括名字和文件格式)
+        response.setHeader("Content-Disposition", "attachment;filename=" + lzmaFileName);
+
+        try(
+            BufferedInputStream inputStream = FileUtil.getInputStream(lzmaFilePath);
+            ServletOutputStream outputStream = response.getOutputStream();
+        ){
+            IoUtil.copy(inputStream, outputStream);
+            //关闭流
+            IoUtil.close(outputStream);
+            IoUtil.close(inputStream);
+        }catch (Exception e){
+            log.error("文件流输出失败,文件路径: " + lzmaFilePath, e);
+            throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
+        }
+
+    }
+}