From 10312758f4375e94832a39cfc5199585832136a0 Mon Sep 17 00:00:00 2001 From: xiaofeng Date: Tue, 4 Jan 2022 16:28:19 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:xmind=E7=89=88=E6=9C=AC=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/FileServiceImpl.java | 9 +- .../com/xiaoju/framework/util/TreeUtil.java | 154 ++++++++---------- 2 files changed, 72 insertions(+), 91 deletions(-) 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..96a6309 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 @@ -306,8 +306,11 @@ 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); + String picXml1 = "attachments"; // 存放图片的文件夹1 + String picXml2 = "resources"; // 获得图片的文件夹2 + LOGGER.info("fileName为:" + fileName); + String picName1 = (fileName + picXml1).replace("/", File.separator); + String picName2 = (fileName + picXml2).replace("/", File.separator); fileName = (fileName + fileXml).replace("/", File.separator); File file = new File(fileName); if(!file.exists()) // 判断文件是否存在 @@ -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, picName1, picName2, 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 a873bac..fdab293 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 @@ -4,36 +4,25 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.xiaoju.framework.constants.enums.ProgressEnum; -import com.xiaoju.framework.constants.enums.StatusCode; -import com.xiaoju.framework.controller.UploadController; -import com.xiaoju.framework.entity.exception.CaseServerException; import com.xiaoju.framework.entity.request.cases.FileImportReq; import com.xiaoju.framework.entity.xmind.*; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringEscapeUtils; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; import org.dom4j.Document; -import org.dom4j.DocumentHelper; import org.dom4j.Element; -import org.dom4j.Namespace; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; import org.springframework.mock.web.MockMultipartFile; import org.springframework.util.StringUtils; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; -import java.net.HttpURLConnection; -import java.net.URL; +import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; +import java.awt.image.BufferedImage; import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; import java.text.SimpleDateFormat; import java.util.*; @@ -395,7 +384,6 @@ public static void exportDataToXml(JSONArray children, Element rootTopic, String } public static void importDataByJson(JSONArray children, JSONObject rootTopic, String fileName, HttpServletRequest requests, String uploadPath) throws IOException { - String vaildName = fileName; JSONObject rootObj = new JSONObject(); JSONObject dataObj = new JSONObject(); JSONArray childrenNext = new JSONArray(); @@ -405,6 +393,7 @@ public static void importDataByJson(JSONArray children, JSONObject rootTopic, St if(rootTopic.containsKey("image")){ // 添加imagesize属性 // 需要将图片传到云空间中,然后将返回的链接导入 Map imageSize = new HashMap<>(); + // todo: 此处直接写死的方式存在问题 imageSize.put("width", "400"); imageSize.put("height", "184"); String image = ""; @@ -412,11 +401,11 @@ public static void importDataByJson(JSONArray children, JSONObject rootTopic, St String path = rootTopic.getJSONObject("image").getString("src"); String[] strs = path.split("/"); int len = strs.length; - fileName = fileName + "/"; image = strs[len-1]; // 此时image为图片所在的本地位置 // 将文件传入到temp文件下,因此需要将文件进行转换,将file文件类型转化为MultipartFile类型,然后进行上传 - File file = new File(fileName + image); + File file = new File(fileName + File.separator + image); FileInputStream fileInputStream = new FileInputStream(file); + MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream); @@ -471,14 +460,14 @@ public static void importDataByJson(JSONArray children, JSONObject rootTopic, St if (rootTopic.containsKey("children") && rootTopic.getJSONObject("children").containsKey("attached")) { JSONArray jsonArray = rootTopic.getJSONObject("children").getJSONArray("attached"); for (int i = 0; i < jsonArray.size(); i++) { - importDataByJson(childrenNext, (JSONObject) jsonArray.get(i), vaildName, requests, uploadPath); + importDataByJson(childrenNext, (JSONObject) jsonArray.get(i), fileName, requests, uploadPath); } } } //导入xml内容 - public static JSONArray importDataByXml(FileImportReq request, Element e, String fileName, HttpServletRequest requests, String uploadPath) throws IOException { + public static JSONArray importDataByXml(FileImportReq request, Element e, String fileName1, String fileName2, HttpServletRequest requests, String uploadPath) throws IOException { JSONArray jsonArray = new JSONArray(); List elementList = e.elements(); if(elementList.size() == 0) @@ -493,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"); @@ -504,51 +492,41 @@ public static JSONArray importDataByXml(FileImportReq request, Element e, String if(element.getName().equalsIgnoreCase("img")){ // 添加imagesize属性 // 需要将图片传到云空间中,然后将返回的链接导入 LOGGER.info("xhtml:img可以使用,其中element中的内容为:" + element); - imageSize.put("width", element.attributeValue("width")); - imageSize.put("height", element.attributeValue("height")); + String path = element.attributeValue("src"); - String[] strs = path.split("/"); - int len = strs.length; - fileName = fileName + "/"; - image = strs[len-1]; // 此时image为图片所在的本地位置 + // 将文件传入到temp文件下,因此需要将文件进行转换,将file文件类型转化为MultipartFile类型,然后进行上传 - File file = new File(fileName + image); - FileInputStream fileInputStream = new FileInputStream(file); - MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), - ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream); - - // 将MultipartFile文件进行上传 - JSONObject ret = new JSONObject(); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/"); - String format = sdf.format(new Date()); - File folder = new File(uploadPath + format);// 文件夹的名字 - if (!folder.isDirectory()) { // 如果文件夹为空,则新建文件夹 - folder.mkdirs(); - } - // 对上传的文件重命名,避免文件重名 - String oldName = multipartFile.getOriginalFilename(); // 获取文件的名字 - String newName = UUID.randomUUID().toString() - + oldName.substring(oldName.lastIndexOf("."), oldName.length()); // 生成新的随机的文件名字 - File newFile = new File(folder, newName); - LOGGER.info("newFile的名字为" + newFile); + File file = null; + File file1 = new File(fileName1 + "/" + path.split("/")[1]); + File file2 = new File(fileName2 + "/" + path.split("/")[1]); + if(file1.exists()) + file = file1; + else + file = file2; + LOGGER.info("file为:" + file); try { - multipartFile.transferTo(newFile); + 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)); + + String fileUrlPath = FileUtil.fileUpload(uploadPath, multipartFile); + // 返回上传文件的访问路径 // request.getScheme()可获取请求的协议名,request.getServerName()可获取请求的域名,request.getServerPort()可获取请求的端口号 String filePath = requests.getScheme() + "://" + requests.getServerName() - + ":" + requests.getServerPort() + "/" + format + newName; + + ":" + requests.getServerPort() + "/" + fileUrlPath; LOGGER.info("filepath的路径为:" + filePath); picPath = filePath; - JSONArray datas = new JSONArray(); - JSONObject data = new JSONObject(); - data.put("url", filePath); - ret.put("success", 1); - datas.add(data); - ret.put("data", datas); - } catch (IOException err) { - LOGGER.error("上传文件失败, 请重试。", err); - ret.put("success", 0); - ret.put("data", ""); + + } catch (Exception err) { + LOGGER.error("图片上传文件失败, 请重试。", err); } } @@ -567,7 +545,7 @@ else if (element.getName().equalsIgnoreCase("title")) { { if(childEle.getName().equalsIgnoreCase("topics")) { - JSONArray jsonArray1 = importDataByXml(request, childEle, fileName, requests, uploadPath); + JSONArray jsonArray1 = importDataByXml(request, childEle, fileName1, fileName2, requests, uploadPath); if(jsonArray1.size()>0){ childrenNext.addAll(jsonArray1); } @@ -595,22 +573,22 @@ else if (element.getName().equalsIgnoreCase("title")) { } - //根据xml文件获取优先级 - private static Integer getPriorityByElement(Element element) - { - Integer priorityId = 0; - Map priorityIds = getAllPriority(); - List markers = element.elements(); - if (markers != null && markers.size() > 0) { - for (Element mark : markers) { - String markId = mark.attributeValue("marker-id"); - if (priorityIds.containsKey(markId)) { - priorityId = priorityIds.get(markId); - } - } - } - return priorityId; - } + //根据xml文件获取优先级 + private static Integer getPriorityByElement(Element element) + { + Integer priorityId = 0; + Map priorityIds = getAllPriority(); + List markers = element.elements(); + if (markers != null && markers.size() > 0) { + for (Element mark : markers) { + String markId = mark.attributeValue("marker-id"); + if (priorityIds.containsKey(markId)) { + priorityId = priorityIds.get(markId); + } + } + } + return priorityId; + } //根据content.json文件获取优先级 private static Integer getPriorityByJsonArray(JSONArray markers) @@ -654,17 +632,17 @@ private static String getPriorityByJson(JSONObject jsonObject) } //获取所有优先级 - private static Map getAllPriority(){ - Map priorityIds = new HashMap<>(); - priorityIds.put("priority-1", 1); - priorityIds.put("priority-2", 2); - priorityIds.put("priority-3", 3); - priorityIds.put("priority-4", 3); - priorityIds.put("priority-5", 3); - priorityIds.put("priority-6", 3); - priorityIds.put("priority-7", 3); - priorityIds.put("priority-8", 3); - priorityIds.put("priority-9", 3); - return priorityIds; - } + private static Map getAllPriority(){ + Map priorityIds = new HashMap<>(); + priorityIds.put("priority-1", 1); + priorityIds.put("priority-2", 2); + priorityIds.put("priority-3", 3); + priorityIds.put("priority-4", 3); + priorityIds.put("priority-5", 3); + priorityIds.put("priority-6", 3); + priorityIds.put("priority-7", 3); + priorityIds.put("priority-8", 3); + priorityIds.put("priority-9", 3); + return priorityIds; + } }