Skip to content

Commit

Permalink
bugfix:xmind版本兼容问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaofeng committed Jan 4, 2022
1 parent 3546577 commit 1031275
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()) // 判断文件是否存在
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, picName1, picName2, requests, uploadPath);
}
return buildCaseCreateReq(request, jsonArray);
}
Expand Down
154 changes: 66 additions & 88 deletions case-server/src/main/java/com/xiaoju/framework/util/TreeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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();
Expand All @@ -405,18 +393,19 @@ public static void importDataByJson(JSONArray children, JSONObject rootTopic, St
if(rootTopic.containsKey("image")){ // 添加imagesize属性
// 需要将图片传到云空间中,然后将返回的链接导入
Map<String, String> imageSize = new HashMap<>();
// todo: 此处直接写死的方式存在问题
imageSize.put("width", "400");
imageSize.put("height", "184");
String image = "";
String picPath = "";
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);

Expand Down Expand Up @@ -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<Element> elementList = e.elements();
if(elementList.size() == 0)
Expand All @@ -493,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 @@ -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);
}
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -595,22 +573,22 @@ else if (element.getName().equalsIgnoreCase("title")) {

}

//根据xml文件获取优先级
private static Integer getPriorityByElement(Element element)
{
Integer priorityId = 0;
Map<String, Integer> priorityIds = getAllPriority();
List<Element> 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<String, Integer> priorityIds = getAllPriority();
List<Element> 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)
Expand Down Expand Up @@ -654,17 +632,17 @@ private static String getPriorityByJson(JSONObject jsonObject)
}

//获取所有优先级
private static Map<String, Integer> getAllPriority(){
Map<String, Integer> 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<String, Integer> getAllPriority(){
Map<String, Integer> 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;
}
}

0 comments on commit 1031275

Please sign in to comment.