管道基础大数据平台系统开发-【后端】-Server
13693261870
2024-04-17 84d5ac6676d471073cc1054536f9ca12801ca86d
集成CAD预览功能
已添加2个文件
已修改3个文件
307 ■■■■■ 文件已修改
src/main/java/com/lf/server/controller/data/MetaController.java 36 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/entity/all/StaticData.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/helper/AsyncHelper.java 63 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/service/show/CadService.java 201 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application.yml 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/controller/data/MetaController.java
@@ -22,13 +22,14 @@
import com.lf.server.service.data.DownloadService;
import com.lf.server.service.data.MetaService;
import com.lf.server.service.data.VerService;
import com.lf.server.service.show.CadService;
import com.lf.server.service.sys.DepService;
import com.lf.server.service.sys.DownlogService;
import com.lf.server.service.sys.TokenService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
@@ -45,26 +46,29 @@
@RestController
@RequestMapping("/meta")
public class MetaController extends BaseController {
    @Autowired
    @Resource
    MetaService metaService;
    @Autowired
    @Resource
    TokenService tokenService;
    @Autowired
    @Resource
    BaseQueryService baseQueryService;
    @Autowired
    @Resource
    DownlogService downlogService;
    @Autowired
    @Resource
    DownloadService downloadService;
    @Autowired
    @Resource
    VerService verService;
    @Autowired
    @Resource
    DepService depService;
    @Resource
    CadService cadService;
    @SysLog()
    @ApiOperation(value = "分页查询并返回记录数")
@@ -247,6 +251,22 @@
    }
    @SysLog()
    @ApiOperation(value = "查询Dwg转换")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "元数据ID", dataType = "int", paramType = "query", example = "7715")
    })
    @GetMapping(value = "/selectConvertToDwg")
    public ResponseMsg<Object> selectConvertToDwg(int id) {
        try {
            String rs = cadService.convert(id);
            return success(rs);
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
    @SysLog()
    @ApiOperation(value = "插入一条")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "entity", value = "实体类", dataType = "MetaEntity", paramType = "body")
src/main/java/com/lf/server/entity/all/StaticData.java
@@ -62,6 +62,8 @@
    public final static int I100 = 100;
    public final static int I120 = 120;
    public static final int I180 = 180;
    public static final int I180_NEG = -180;
@@ -92,6 +94,8 @@
    public final static String IN = "in";
    public final static String DWG = "dwg";
    public final static String ZIP = ".zip";
    public final static String XLS = ".xls";
src/main/java/com/lf/server/helper/AsyncHelper.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,63 @@
package com.lf.server.helper;
import com.lf.server.entity.all.StaticData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
 * å¼‚步帮助类
 * @author WWW
 * @date 2023-07-10
 */
public class AsyncHelper {
    /**
     * æ“ä½œå»¶è¿Ÿ10毫秒
     */
    private final static int OPERATE_DELAY_TIME = 10;
    private final static Log log = LogFactory.getLog(AsyncHelper.class);
    private ScheduledExecutorService executor = SpringContextHelper.getBean("scheduledExecutorService");
    public AsyncHelper() {
    }
    /**
     * æ‰§è¡Œä»»åŠ¡
     */
    public void execute(TimerTask task) {
        executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
    }
    /**
     * å…³é—­ä»»åŠ¡
     */
    public void shutdown() {
        shutdownAndAwaitTermination(executor);
    }
    /**
     * åœæ­¢ä»»åŠ¡çº¿ç¨‹æ± 
     */
    public static void shutdownAndAwaitTermination(ExecutorService pool) {
        try {
            if (null == pool || pool.isShutdown()) {
                return;
            }
            pool.shutdown();
            if (!pool.awaitTermination(StaticData.I120, TimeUnit.SECONDS)) {
                pool.shutdownNow();
            }
        } catch (InterruptedException ie) {
            pool.shutdownNow();
            Thread.currentThread().interrupt();
        }
    }
}
src/main/java/com/lf/server/service/show/CadService.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,201 @@
package com.lf.server.service.show;
import com.lf.server.entity.all.StaticData;
import com.lf.server.entity.data.MetaEntity;
import com.lf.server.helper.PathHelper;
import com.lf.server.service.data.MetaService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.io.*;
/**
 * CAD服务类
 * @author WWW
 * @date 2024-04-17
 */
@SuppressWarnings("ALL")
@Service
public class CadService {
    @Value("${sys.cad.exePath}")
    private String exePath;
    @Value("${sys.cad.targetPath}")
    private String targetPath;
    @Resource
    MetaService metaService;
    @Resource
    PathHelper pathHelper;
    private final static Log log = LogFactory.getLog(CadService.class);
    /**
     * æ ¼å¼è½¬åŒ–
     */
    public String convert(int metaId) {
        MetaEntity me = metaService.selectById(metaId);
        if (null == me || StringUtils.isEmpty(me.getPath()) || !StaticData.DWG.equals(me.getType())) {
            return null;
        }
        String dwg = pathHelper.getConfig().getUploadPath() + File.separator + me.getPath();
        File file = new File(dwg);
        if (!file.exists() || file.isDirectory()) {
            return null;
        }
        String[] strs = me.getPath().replace("\\", "/").split("/");
        String outPath = targetPath + "/" + strs[0];
        String outName = strs[1].replace(".dwg", ".mxweb");
        file = new File(outPath + "/" + outName);
        if (file.exists()) {
            return strs[0] + "/" + outName;
        }
        if (callConvert(dwg, outPath, outName)) {
            return strs[0] + "/" + outName;
        }
        return null;
    }
    public boolean callConvert(String dwgFile, String outPath, String outName) {
        try {
            String jsonParam = "{'srcpath':'" + dwgFile + "','outpath':'" + outPath + "','outname':'" + outName + "'}";
            File f = new File(outPath);
            if (!f.exists() || !f.isDirectory()) {
                f.mkdirs();
            }
            StringBuilder sb = new StringBuilder();
            Process process = Runtime.getRuntime().exec(new String[]{exePath, jsonParam});
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            reader.close();
            return true;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return false;
        }
    }
    public boolean callConvert(String dwgFile) {
        try {
            StringBuilder sb = new StringBuilder();
            String jsonParam = "{\"srcpath\":\"" + dwgFile + "\"}";
            Process process = Runtime.getRuntime().exec(new String[]{exePath, jsonParam});
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            reader.close();
            return true;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return false;
        }
    }
    @SuppressWarnings("AlibabaAvoidManuallyCreateThread")
    public String callMxFileConvert(String sDwgFile) {
        Runtime rn = Runtime.getRuntime();
        Process process = null;
        // è½¬æ¢å‚数。
        String sJsonParam = "{\"srcpath\":\"" + sDwgFile + "\"}";
        String[] sRetJson = new String[1];
        try {
            // å¯åŠ¨ä¸€ä¸ªè¿›ç¨‹åºï¼Œè°ƒç”¨è½¬æ¢ç¨‹åºã€‚
            process = rn.exec(new String[]{exePath, sJsonParam});
            final InputStream ins = process.getInputStream();
            final InputStream errs = process.getErrorStream();
            //确保子进程与主进程之间inputStream不阻塞
            new Thread() {
                @Override
                public void run() {
                    BufferedReader inb = null;
                    String line = null;
                    try {
                        inb = new BufferedReader(new InputStreamReader(ins, "gbk"));
                        while ((line = inb.readLine()) != null) {
                            sRetJson[0] = line;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (null != inb) {
                                inb.close();
                            }
                            if (null != ins) {
                                ins.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
            new Thread() {
                @Override
                public void run() {
                    BufferedReader errb = null;
                    String line = null;
                    try {
                        errb = new BufferedReader(new InputStreamReader(errs, "gbk"));
                        while ((line = errb.readLine()) != null) {
                            System.out.println("executeMxExe - ErrorStream : " + line);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (null != errb) {
                                errb.close();
                            }
                            if (null != errs) {
                                errs.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
            process.waitFor();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (null != process) {
                OutputStream out = process.getOutputStream();
                if (null != out) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        log.error(e.getMessage(), e);
                    }
                }
                process.destroy();
            }
        }
        return sRetJson[0];
    }
}
src/main/resources/application.yml
@@ -153,5 +153,8 @@
    upload: D:\LF\upload
    # ä¸´æ—¶ç›®å½•
    temp: D:\LF\temp
  cad:
    exePath: C:/360/MxDrawCloudServer1.0TryVersion/MxDrawCloudServer/Bin/MxCAD/Release/mxcadassembly.exe
    targetPath: C:/360/MxDrawCloudServer1.0TryVersion/MxDrawCloudServer/SRC/TsWeb/public/data
  # ä¸Šä¼ é™„件表
  attachTabs: bd.b_pac_hydrogeology,bd.b_pac_frozensoil,bd.b_pac_geologic_hazard,bd.b_pac_marine_meteorological,bd.b_pac_meteorological,bs.m_equipment_nameplate,bs.m_hydraulic_protection,bs.m_marker,bs.s_explorationpoint,bs.u_sectionline