|
@@ -7,6 +7,7 @@ import edu.nju.util.TransUtil;
|
|
|
import org.json.JSONArray;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.lang.reflect.Field;
|
|
@@ -14,11 +15,9 @@ import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashSet;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.*;
|
|
|
import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipFile;
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
/**
|
|
@@ -30,7 +29,7 @@ import java.util.zip.ZipOutputStream;
|
|
|
public class DataService {
|
|
|
private static final int BUFFER_SIZE = 2 * 1024;
|
|
|
|
|
|
- private static final String imageUrlPrefix="https://mooctest-site.oss-cn-shanghai.aliyuncs.com/xinchuang/image/";
|
|
|
+ private static final String imageUrlPrefix = "https://mooctest-site.oss-cn-shanghai.aliyuncs.com/xinchuang/image/";
|
|
|
@Autowired
|
|
|
ExamDao examDao;
|
|
|
|
|
@@ -59,6 +58,7 @@ public class DataService {
|
|
|
BugDetailDao bugDetailDao;
|
|
|
|
|
|
private String cpSerialNum = "cp_ent_dev";
|
|
|
+ private String nginxImageUrlPrefix = "/xinchuangdata/input/imageUnzip";
|
|
|
|
|
|
/**
|
|
|
* 根据caseId获取对应bug信息
|
|
@@ -171,16 +171,28 @@ public class DataService {
|
|
|
return bugDetailList;
|
|
|
}
|
|
|
|
|
|
- public List<BugDetail> saveBugDetailFromOss(String jsonFilePath, String originalCaseId,String cpSerialNum) {
|
|
|
+ public List<BugDetail> saveBugDetailFromFile(MultipartFile sourceZipFile, MultipartFile sourceJsonFile, String originalCaseId, String cpSerialNum) {
|
|
|
try {
|
|
|
- //从oss下载json文件
|
|
|
- URL url = new URL(jsonFilePath);
|
|
|
- URLConnection urlConnection = url.openConnection();
|
|
|
- HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
|
|
|
- httpURLConnection.setConnectTimeout(1000 * 5);
|
|
|
- httpURLConnection.setRequestProperty("Charset", "UTF-8");
|
|
|
- httpURLConnection.connect();
|
|
|
- BufferedInputStream bufferedInputStream = new BufferedInputStream(httpURLConnection.getInputStream());
|
|
|
+ //读取文件流并保存在本地
|
|
|
+ String prefix = "";
|
|
|
+ String zipFilePath = prefix + "/xinchuangdata/input/imageZip/" + originalCaseId + "/" + cpSerialNum + "/" + originalCaseId + ".zip";
|
|
|
+ File zipFile = new File(zipFilePath);
|
|
|
+ if (!zipFile.getParentFile().exists()) {
|
|
|
+ zipFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ if (!sourceZipFile.isEmpty()) {
|
|
|
+ sourceZipFile.transferTo(zipFile);
|
|
|
+ }
|
|
|
+ String jsonFilePath = prefix + "/xinchuangdata/input/" + originalCaseId + "/" + cpSerialNum + "/" + originalCaseId + ".json";
|
|
|
+ File jsonFile = new File(jsonFilePath);
|
|
|
+ if (!jsonFile.getParentFile().exists()) {
|
|
|
+ jsonFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ if (!sourceJsonFile.isEmpty()) {
|
|
|
+ sourceJsonFile.transferTo(jsonFile);
|
|
|
+ }
|
|
|
+ //读取本地文件
|
|
|
+ BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(jsonFile));
|
|
|
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
|
|
int result = bufferedInputStream.read();
|
|
|
while (result != -1) {
|
|
@@ -192,24 +204,34 @@ public class DataService {
|
|
|
List<BugDetail> bugDetailList = JSON.parseArray(json, BugDetail.class);
|
|
|
for (BugDetail bugDetail : bugDetailList) {
|
|
|
bugDetail.setOriginalCaseId(originalCaseId);
|
|
|
- //修改图片文件路径为oss路径
|
|
|
- String imageUrl=bugDetail.getImgUrl();
|
|
|
- String[] imageUrlArray=imageUrl.split(",");
|
|
|
- StringBuilder stringBuilder=new StringBuilder();
|
|
|
- for(String imageUrlStr:imageUrlArray){
|
|
|
- String[] filePath=imageUrlStr.split("/");
|
|
|
- String fileName=filePath[filePath.length-1];
|
|
|
- String newImageUrl=imageUrlPrefix+originalCaseId+"/"+cpSerialNum+"/"+fileName;
|
|
|
- stringBuilder.append(newImageUrl).append(",");
|
|
|
+ //修改图片文件路径为本地路径
|
|
|
+ String imageUrl = bugDetail.getImgUrl();
|
|
|
+ String[] imageUrlArray = imageUrl.split(",");
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ for (String imageUrlStr : imageUrlArray) {
|
|
|
+ if (!"".equals(imageUrl)) {
|
|
|
+ String[] filePath = imageUrlStr.split("/");
|
|
|
+ String fileName = filePath[filePath.length - 1];
|
|
|
+ String newImageUrl = nginxImageUrlPrefix + originalCaseId + "/" + cpSerialNum + "/" + fileName;
|
|
|
+ stringBuilder.append(newImageUrl).append(",");
|
|
|
+ }
|
|
|
}
|
|
|
bugDetail.setImgUrl(stringBuilder.toString());
|
|
|
bugDetailDao.save(bugDetail);
|
|
|
}
|
|
|
+ //解压图片文件,保存至本地
|
|
|
+ String destPath = prefix + "/xinchuangdata/input/imageUnzip/" + originalCaseId + "/" + cpSerialNum;
|
|
|
+ File unzipFile = new File(destPath);
|
|
|
+ if (!unzipFile.getParentFile().exists()) {
|
|
|
+ unzipFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ unZip(zipFile, destPath, originalCaseId, cpSerialNum);
|
|
|
return bugDetailList;
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return new ArrayList<>();
|
|
|
+
|
|
|
}
|
|
|
|
|
|
private void bugDetailToFile(List<BugDetail> bugDetailList, String caseId) {
|
|
@@ -228,7 +250,9 @@ public class DataService {
|
|
|
String sourceFile = "/xinchuangdata/image/" + caseId;
|
|
|
try {
|
|
|
File dest = new File("/xinchuangdata/output/imageZip/" + caseId + ".zip");
|
|
|
- if(!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); }
|
|
|
+ if (!dest.getParentFile().exists()) {
|
|
|
+ dest.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
FileOutputStream fileOutputStream = new FileOutputStream(dest);
|
|
|
toZip(sourceFile, fileOutputStream, false);
|
|
|
} catch (FileNotFoundException e) {
|
|
@@ -241,7 +265,9 @@ public class DataService {
|
|
|
private void exportJson(List<BugDetail> bugDetailList, String caseId) {
|
|
|
try {
|
|
|
File file = new File("/xinchuangdata/output/" + caseId + "/" + caseId + ".json");
|
|
|
- if(!file.getParentFile().exists()) { file.getParentFile().mkdirs(); }
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
JSONArray jsonArray = new JSONArray(bugDetailList);
|
|
|
Writer write = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
|
|
|
write.write(jsonArray.toString());
|
|
@@ -256,7 +282,9 @@ public class DataService {
|
|
|
private <T> void exportCsv(String[] titles, List<T> list, String caseId) {
|
|
|
try {
|
|
|
File file = new File("/xinchuangdata/output/" + caseId + "/" + caseId + ".csv");
|
|
|
- if(!file.getParentFile().exists()) {file.getParentFile().mkdirs(); }
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
//构建输出流,同时指定编码
|
|
|
OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
|
|
|
//csv文件是逗号分隔,除第一个外,每次写入一个单元格数据后需要输入逗号
|
|
@@ -365,6 +393,63 @@ public class DataService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private void unZip(File srcFile, String destDirPath, String originalCaseId, String fromCpSerialNum) throws RuntimeException {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ // 判断源文件是否存在
|
|
|
+ if (!srcFile.exists()) {
|
|
|
+ throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
|
|
|
+ }
|
|
|
+ // 开始解压
|
|
|
+ ZipFile zipFile = null;
|
|
|
+ try {
|
|
|
+ zipFile = new ZipFile(srcFile);
|
|
|
+ Enumeration<?> entries = zipFile.entries();
|
|
|
+
|
|
|
+ while (entries.hasMoreElements()) {
|
|
|
+ ZipEntry entry = (ZipEntry) entries.nextElement();
|
|
|
+ System.out.println("解压" + entry.getName());
|
|
|
+ // 如果是文件夹,就创建个文件夹
|
|
|
+ if (entry.isDirectory()) {
|
|
|
+ String dirPath = destDirPath + "/" + entry.getName();
|
|
|
+ File dir = new File(dirPath);
|
|
|
+ dir.mkdirs();
|
|
|
+ } else {
|
|
|
+ // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
|
|
|
+ File targetFile = new File(destDirPath + "/" + entry.getName());
|
|
|
+ // 保证这个文件的父文件夹必须要存在
|
|
|
+ if (!targetFile.getParentFile().exists()) {
|
|
|
+ targetFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ targetFile.createNewFile();
|
|
|
+ // 将压缩文件内容写入到这个文件中
|
|
|
+ InputStream is = zipFile.getInputStream(entry);
|
|
|
+ FileOutputStream fos = new FileOutputStream(targetFile);
|
|
|
+ int len;
|
|
|
+ byte[] buf = new byte[BUFFER_SIZE];
|
|
|
+ while ((len = is.read(buf)) != -1) {
|
|
|
+ fos.write(buf, 0, len);
|
|
|
+ }
|
|
|
+ // 关流顺序,先打开的后关闭
|
|
|
+ fos.close();
|
|
|
+ is.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ System.out.println("解压完成,耗时:" + (end - start) + " ms");
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("unzip error from ZipUtils", e);
|
|
|
+ } finally {
|
|
|
+ if (zipFile != null) {
|
|
|
+ try {
|
|
|
+ zipFile.close();
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|