package com.terra.system.service.show;
|
|
import com.terra.common.entity.all.StaticData;
|
import com.terra.system.entity.data.MetaEntity;
|
import com.terra.system.entity.sys.AttachEntity;
|
import com.terra.system.helper.PathHelper;
|
import com.terra.system.helper.StringHelper;
|
import com.terra.system.service.data.MetaService;
|
import com.terra.system.service.sys.AttachService;
|
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
|
AttachService attachService;
|
|
@Resource
|
PathHelper pathHelper;
|
|
private final static Log log = LogFactory.getLog(CadService.class);
|
|
/**
|
* 格式转化
|
*/
|
public String convert(int id, Boolean isAttach) {
|
String path = getFilePath(id, isAttach);
|
if (StringHelper.isEmpty(path)) {
|
return "";
|
}
|
|
String dwg = pathHelper.getConfig().getUploadPath() + File.separator + path;
|
File file = new File(dwg);
|
if (!file.exists() || file.isDirectory()) {
|
return "";
|
}
|
|
String[] strs = path.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 "";
|
}
|
|
private String getFilePath(int id, Boolean isAttach) {
|
if (null == isAttach || !isAttach) {
|
MetaEntity me = metaService.selectById(id);
|
if (null == me || StringUtils.isEmpty(me.getPath()) || !StaticData.DWG.equals(me.getType())) {
|
return null;
|
}
|
|
return me.getPath();
|
}
|
|
AttachEntity ae = attachService.selectById(id);
|
if (null == ae || StringUtils.isEmpty(ae.getPath()) || !ae.getPath().toLowerCase().contains(StaticData.DWG)) {
|
return null;
|
}
|
|
return ae.getPath();
|
}
|
|
/**
|
* 调用格式转换
|
*/
|
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];
|
}
|
}
|