|
@@ -17,10 +17,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;
|
|
|
|
|
|
/**
|
|
|
* @Author JiaWei Xu
|
|
@@ -29,6 +28,7 @@ import java.util.Set;
|
|
|
*/
|
|
|
@Service
|
|
|
public class DataService {
|
|
|
+ private static final int BUFFER_SIZE = 2048;
|
|
|
@Autowired
|
|
|
ExamDao examDao;
|
|
|
|
|
@@ -59,7 +59,7 @@ public class DataService {
|
|
|
@Value("${cpSerialNum}")
|
|
|
private String cpSerialNum;
|
|
|
|
|
|
- private static final String bucketName = "mooctest-share";
|
|
|
+ private static final String bucketName = "mooctest-site";
|
|
|
private static final String imageUrlPrefix = "https://mooctest-site.oss-cn-shanghai.aliyuncs.com/xinchuang/image/";
|
|
|
|
|
|
|
|
@@ -174,7 +174,7 @@ public class DataService {
|
|
|
return bugDetailList;
|
|
|
}
|
|
|
|
|
|
- public List<BugDetail> saveBugDetailFromOss(String jsonFilePath, String originalCaseId, String cpSerialNum) {
|
|
|
+ public List<BugDetail> saveBugDetailFromOss(String zipFilePath,String jsonFilePath, String originalCaseId, String cpSerialNum) {
|
|
|
try {
|
|
|
//从oss下载json文件
|
|
|
URL url = new URL(jsonFilePath);
|
|
@@ -208,6 +208,12 @@ public class DataService {
|
|
|
bugDetail.setImgUrl(stringBuilder.toString());
|
|
|
bugDetailDao.save(bugDetail);
|
|
|
}
|
|
|
+ //解压图片文件,上传至oss
|
|
|
+ File zipFile=new File(zipFilePath);
|
|
|
+ String destPath="xinchuangdata/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();
|
|
@@ -226,8 +232,8 @@ public class DataService {
|
|
|
"case_take_id", "originalCaseId", "cpSerialNum"};
|
|
|
File csvFile = exportCsv(titles, bugDetailList, caseId);
|
|
|
File jsonFile = exportJson(bugDetailList, caseId);
|
|
|
- uploadToOss(csvFile);
|
|
|
- uploadToOss(jsonFile);
|
|
|
+// uploadToOss(csvFile);
|
|
|
+// uploadToOss(jsonFile);
|
|
|
}
|
|
|
|
|
|
private File exportJson(List<BugDetail> bugDetailList, String caseId) {
|
|
@@ -286,16 +292,87 @@ public class DataService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- private void uploadToOss(File file) {
|
|
|
+ private static void uploadToOss(String objectName,File file) {
|
|
|
if (file != null) {
|
|
|
- OSS ossClient = OssAliyun.initHangZhouOss();
|
|
|
- String objectName = "crowd-dataset/" + file.getName();
|
|
|
+ OSS ossClient = OssAliyun.initShangHaiOss();
|
|
|
OssAliyun.uploadFile(ossClient, bucketName, objectName, file);
|
|
|
} else {
|
|
|
System.out.println("file is null");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * zip解压
|
|
|
+ *
|
|
|
+ * @param srcFile zip源文件
|
|
|
+ * @param destDirPath 解压后的目标文件夹
|
|
|
+ * @throws RuntimeException 解压失败会抛出运行时异常
|
|
|
+ */
|
|
|
+
|
|
|
+ public static 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();
|
|
|
+ //图片文件上传至oss
|
|
|
+ String objectName = "xinchuang/image/"+originalCaseId+"/"+fromCpSerialNum+"/" + targetFile.getName();
|
|
|
+ uploadToOss(objectName,targetFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// public static void main(String[] args){
|
|
|
+// File a=new File("/Users/xujiawei/crowd/crowdsource-backend/data/a.zip");
|
|
|
+// unZip(a,"/Users/xujiawei/crowd/crowdsource-backend/data/test","1","test");
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
|