Skip to content

Commit

Permalink
bugfixz:导入xmind路径问题适配
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaofeng committed Nov 27, 2021
1 parent 09e9272 commit 52935ad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoju.framework.constants.enums.StatusCode;
import com.xiaoju.framework.entity.persistent.TestCase;
import com.xiaoju.framework.entity.exception.CaseServerException;
import com.xiaoju.framework.entity.persistent.TestCase;
import com.xiaoju.framework.entity.request.cases.CaseCreateReq;
import com.xiaoju.framework.entity.request.cases.FileImportReq;
import com.xiaoju.framework.entity.response.cases.ExportXmindResp;
import com.xiaoju.framework.handler.RecordRoom;
import com.xiaoju.framework.mapper.TestCaseMapper;
import com.xiaoju.framework.service.CaseService;
import com.xiaoju.framework.service.FileService;
Expand All @@ -20,7 +19,9 @@
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
Expand All @@ -30,7 +31,9 @@
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xiaoju.framework.constants.SystemConstant.POINT;
import static com.xiaoju.framework.constants.XmindConstant.*;
Expand Down Expand Up @@ -306,10 +309,10 @@ public CaseCreateReq buildCaseByXml(FileImportReq request, String fileName, Http

JSONArray jsonArray = new JSONArray();
String fileXml = "content.xml";
String picXml = "attachments"; // 存放图片的文件夹
String picName = (fileName + picXml).replace("/", File.separator);
fileName = (fileName + fileXml).replace("/", File.separator);
File file = new File(fileName);
// String picXml = "attachments"; // 存放图片的文件夹
// String picName = (fileName + picXml).replace("/", File.separator);
String contentFullName = (fileName + fileXml).replace("/", File.separator);
File file = new File(contentFullName);
if(!file.exists()) // 判断文件是否存在
throw new CaseServerException("导入失败,文件不存在", StatusCode.FILE_IMPORT_ERROR);
SAXReader reade = new SAXReader();
Expand All @@ -320,7 +323,7 @@ public CaseCreateReq buildCaseByXml(FileImportReq request, String fileName, Http
String eleName = childElement.getName();
if(eleName.equalsIgnoreCase("sheet"))
{
jsonArray = TreeUtil.importDataByXml(request, childElement, picName, requests, uploadPath);
jsonArray = TreeUtil.importDataByXml(request, childElement, fileName, requests, uploadPath);
}
return buildCaseCreateReq(request, jsonArray);
}
Expand Down
28 changes: 12 additions & 16 deletions case-server/src/main/java/com/xiaoju/framework/util/TreeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,6 @@ public static JSONArray importDataByXml(FileImportReq request, Element e, String
List<Element> newList = element1.elements();
Map<String, String> imageSize = new HashMap<>();
String text = "";
String image = "";
String picPath = "";
Integer priorityId = 0;
String created = element1.attributeValue("timestamp");
Expand All @@ -495,24 +494,21 @@ public static JSONArray importDataByXml(FileImportReq request, Element e, String
LOGGER.info("xhtml:img可以使用,其中element中的内容为:" + element);

String path = element.attributeValue("src");
String[] strs = path.split("/");
int len = strs.length;
image = strs[len-1]; // 此时image为图片所在的本地位置

// 将文件传入到temp文件下,因此需要将文件进行转换,将file文件类型转化为MultipartFile类型,然后进行上传
File file = new File(fileName + File.separator + image);

if (StringUtils.isEmpty(element.attributeValue("width")) || StringUtils.isEmpty(element.attributeValue("height"))) {
BufferedImage sourceImg = ImageIO.read(new FileInputStream(file));
imageSize.put("width", String.valueOf(sourceImg.getWidth()));
imageSize.put("height", String.valueOf(sourceImg.getHeight()));
} else {
imageSize.put("width", element.attributeValue("width"));
imageSize.put("height", element.attributeValue("height"));
}
File file = new File(fileName + path.split(":")[1]);
try {
if (StringUtils.isEmpty(element.attributeValue("width")) || StringUtils.isEmpty(element.attributeValue("height"))) {
BufferedImage sourceImg = ImageIO.read(new FileInputStream(file));
imageSize.put("width", String.valueOf(sourceImg.getWidth()));
imageSize.put("height", String.valueOf(sourceImg.getHeight()));
} else {
imageSize.put("width", element.attributeValue("width"));
imageSize.put("height", element.attributeValue("height"));
}

MultipartFile multipartFile = new MockMultipartFile(file.getName(), new FileInputStream(file));
MultipartFile multipartFile = new MockMultipartFile(file.getName(), new FileInputStream(file));

try {
String fileUrlPath = FileUtil.fileUpload(uploadPath, multipartFile);

// 返回上传文件的访问路径
Expand Down

0 comments on commit 52935ad

Please sign in to comment.