Selaa lähdekoodia

Merge remote-tracking branch 'origin/test' into test

lyhzzz 1 vuosi sitten
vanhempi
commit
27322e6fbc

+ 20 - 7
src/main/java/com/fdkankan/scene/controller/FileConvertController.java

@@ -28,7 +28,7 @@ public class FileConvertController extends BaseController {
     /**
      * <p>
             TODO
-     * </p>      
+     * </p>
      * @author dengsixing
      * @date 2022/8/1
      * @param file
@@ -43,10 +43,10 @@ public class FileConvertController extends BaseController {
     /**
      * <p>
             TODO
-     * </p>      
+     * </p>
      * @author dengsixing
-     * @date 2022/8/1 
-     * @param file 
+     * @date 2022/8/1
+     * @param file
      **/
     @PostMapping("/convertTxtToDam")
     public void convertTxtToDam(@RequestParam("file") MultipartFile file) throws Exception{
@@ -56,14 +56,27 @@ public class FileConvertController extends BaseController {
     /**
      * <p>
             TODO
-     * </p>      
+     * </p>
      * @author dengsixing
-     * @date 2022/8/1 
-     * @param file 
+     * @date 2022/8/1
+     * @param file
      **/
     @PostMapping("/convertTxtToLzma")
     public void convertTxtToLzma(@RequestParam("file") MultipartFile file) throws Exception{
         fileConvertService.convertTxtToLzma(file, this.response);
     }
 
+    /**
+     * <p>
+     TODO
+     * </p>
+     * @author dengsixing
+     * @date 2022/8/1
+     * @param file
+     **/
+    @PostMapping("/convertDamToTxt")
+    public void convertDamToTxt(@RequestParam("file") MultipartFile file) throws Exception{
+        fileConvertService.convertDamToTxt(file, this.response);
+    }
+
 }

+ 2 - 0
src/main/java/com/fdkankan/scene/service/IFileConvertService.java

@@ -17,6 +17,8 @@ public interface IFileConvertService {
 
     void convertTxtToDam(MultipartFile file, HttpServletResponse response) throws Exception;
 
+    void convertDamToTxt(MultipartFile file, HttpServletResponse response) throws Exception;
+
     void convertTxtToLzma(MultipartFile file, HttpServletResponse response) throws Exception;
 
 

+ 47 - 0
src/main/java/com/fdkankan/scene/service/impl/FileConvertServiceImpl.java

@@ -128,6 +128,53 @@ public class FileConvertServiceImpl implements IFileConvertService {
     }
 
     @Override
+    public void convertDamToTxt(MultipartFile file, HttpServletResponse response) throws Exception {
+        //生成uuid
+        String fileName = "modeldata.txt";
+        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(!"dam".equals(type)){
+            throw new BusinessException(ErrorCode.FAILURE_CODE_7007, "dam");
+        }
+        CreateObjUtil.convertDamToTxt(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