diff --git a/case-server/src/main/java/com/xiaoju/framework/service/impl/FileServiceImpl.java b/case-server/src/main/java/com/xiaoju/framework/service/impl/FileServiceImpl.java index 8359c28..baba3f4 100644 --- a/case-server/src/main/java/com/xiaoju/framework/service/impl/FileServiceImpl.java +++ b/case-server/src/main/java/com/xiaoju/framework/service/impl/FileServiceImpl.java @@ -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; @@ -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; @@ -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.*; @@ -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(); @@ -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); } diff --git a/case-server/src/main/java/com/xiaoju/framework/util/TreeUtil.java b/case-server/src/main/java/com/xiaoju/framework/util/TreeUtil.java index d199bc0..a7a90dc 100644 --- a/case-server/src/main/java/com/xiaoju/framework/util/TreeUtil.java +++ b/case-server/src/main/java/com/xiaoju/framework/util/TreeUtil.java @@ -482,7 +482,6 @@ public static JSONArray importDataByXml(FileImportReq request, Element e, String List newList = element1.elements(); Map imageSize = new HashMap<>(); String text = ""; - String image = ""; String picPath = ""; Integer priorityId = 0; String created = element1.attributeValue("timestamp"); @@ -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); // 返回上传文件的访问路径