From 68ca3282657c00cab5423d62b8db7c2d79a1c26c Mon Sep 17 00:00:00 2001
From: youys <1272586223@qq.com>
Date: Tue, 5 Jul 2022 09:32:58 +0800
Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E4=BA=8E=E8=B4=A8=E9=87=8F=E8=AF=84?=
=?UTF-8?q?=E5=88=86=E5=AF=BC=E5=87=BAExcel?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 6 ++
.../net/educoder/ecsonar/model/Metrics.java | 11 ++-
.../ecsonar/services/SonarService.java | 5 +-
.../ecsonar/task/ReadExcelRunnable.java | 61 ++++++++++++-----
.../net/educoder/ecsonar/utils/SslUtils.java | 67 +++++++++++++++++++
src/main/resources/application.properties | 2 +-
.../ecsonar/EcsonarApplicationTests.java | 32 +++++++--
7 files changed, 159 insertions(+), 25 deletions(-)
create mode 100644 src/main/java/net/educoder/ecsonar/utils/SslUtils.java
diff --git a/pom.xml b/pom.xml
index 62f400a..1fce790 100644
--- a/pom.xml
+++ b/pom.xml
@@ -63,6 +63,12 @@
5.7.19
+
+ cn.hutool
+ hutool-http
+ 5.7.19
+
+
com.alibaba
fastjson
diff --git a/src/main/java/net/educoder/ecsonar/model/Metrics.java b/src/main/java/net/educoder/ecsonar/model/Metrics.java
index e80c039..985cb16 100644
--- a/src/main/java/net/educoder/ecsonar/model/Metrics.java
+++ b/src/main/java/net/educoder/ecsonar/model/Metrics.java
@@ -31,7 +31,7 @@ public class Metrics {
public String getComplexity() {
- return complexity+"";
+ return complexity == null ? "0" : complexity+"";
}
public void setComplexity(Float complexity) {
@@ -79,6 +79,9 @@ public class Metrics {
}
public String getViolations() {
+ if(lines <= 0){
+ return "-";
+ }
return violations;
}
@@ -119,6 +122,9 @@ public class Metrics {
}
public String getBugs() {
+ if(lines <= 0){
+ return "-";
+ }
return bugs;
}
@@ -159,6 +165,9 @@ public class Metrics {
}
public String getCode_smells() {
+ if(lines <= 0){
+ return "-";
+ }
return code_smells;
}
diff --git a/src/main/java/net/educoder/ecsonar/services/SonarService.java b/src/main/java/net/educoder/ecsonar/services/SonarService.java
index d135495..521fb77 100644
--- a/src/main/java/net/educoder/ecsonar/services/SonarService.java
+++ b/src/main/java/net/educoder/ecsonar/services/SonarService.java
@@ -234,7 +234,7 @@ public class SonarService {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
} catch (IOException e) {
- log.error("文件下载失败: {}-{}", zipUrl, zipPath);
+ log.error("文件下载失败: {}-{}", zipUrl, zipPath, e);
throw e;
}
@@ -248,13 +248,14 @@ public class SonarService {
*/
public void scanExcel(String excelPath) {
- int homeworkId = 20210101;
+ int homeworkId = 557155;
try {
List personList = ExcelUtil.readExcel(excelPath);
Map> collect = personList.stream().collect(Collectors.groupingBy(Person::getUid));
+ log.info("共有:{}位学生提交的代码", collect.size());
collect.forEach((k, v) -> {
SonarRequest request = new SonarRequest();
request.setHomeworkId(homeworkId);
diff --git a/src/main/java/net/educoder/ecsonar/task/ReadExcelRunnable.java b/src/main/java/net/educoder/ecsonar/task/ReadExcelRunnable.java
index 6df53d1..45f0a32 100644
--- a/src/main/java/net/educoder/ecsonar/task/ReadExcelRunnable.java
+++ b/src/main/java/net/educoder/ecsonar/task/ReadExcelRunnable.java
@@ -1,7 +1,15 @@
package net.educoder.ecsonar.task;
+import cn.hutool.core.util.RandomUtil;
+import cn.hutool.core.util.RuntimeUtil;
+import cn.hutool.core.util.ZipUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
+import net.educoder.ecsonar.model.api.Person;
import net.educoder.ecsonar.model.api.SonarRequest;
import net.educoder.ecsonar.services.SonarService;
+import net.educoder.ecsonar.utils.SslUtils;
+import net.educoder.ecsonar.utils.UrlUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -10,8 +18,11 @@ import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
+import java.net.URLDecoder;
+
/**
* @Author: youys
@@ -39,15 +50,15 @@ public class ReadExcelRunnable implements Runnable{
String path = String.format("/tmp/%d/%s/", homeworkId, uid);
File file = new File(path);
if(!file.exists()){
- file.mkdir();
+ file.mkdirs();
}
// for (Person person : sonarRequest.getPersonList()) {
// String[] split = StringUtils.split(person.getDownloadUrl(), "/");
// String zipFilename = UrlUtil.getURLDecoderString(split[split.length - 1]);
// try {
// download(person.getDownloadUrl(),path+zipFilename);
-// } catch (IOException e) {
-// e.printStackTrace();
+// } catch (Exception e) {
+// log.error("download exception", e);
// }
// }
String key = String.format("%d-%s", homeworkId, uid);
@@ -55,11 +66,13 @@ public class ReadExcelRunnable implements Runnable{
sonarService.sonar(path,key);
}
- private void download(String zipUrl, String zipPath) throws IOException {
+ private void download(String zipUrl, String zipPath) throws Exception {
log.info("下载文件: {}-{}", zipUrl, zipPath);
- String cookie = "_educoder_session=1f4bf34409fec2180b99aa1cbf0b7586";
+
+ String cookie = "_educoder_session=2237164bc8ff253aa43c8e089015da7a";
URL url = new URL(zipUrl);
+ SslUtils.ignoreSsl();
URLConnection conn = url.openConnection();
conn.setRequestProperty("Cookie", cookie);
conn.setDoInput(true);
@@ -67,19 +80,37 @@ public class ReadExcelRunnable implements Runnable{
String fileName = conn.getHeaderField("Content-Disposition");
if(StringUtils.isNotEmpty(fileName)){
fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
+ fileName = URLDecoder.decode(fileName,"UTF-8");
fileName = fileName.substring(fileName.indexOf("filename=")+10,fileName.length()-1);
zipPath = zipPath.substring(0, zipPath.lastIndexOf("/")+1) + fileName;
}
- try (BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
- FileOutputStream fileOutputStream = new FileOutputStream(zipPath)) {
- byte dataBuffer[] = new byte[1024];
- int bytesRead;
- while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
- fileOutputStream.write(dataBuffer, 0, bytesRead);
- }
- } catch (IOException e) {
- log.error("文件下载失败: {}-{}", zipUrl, zipPath);
- throw e;
+
+// if(StringUtils.isNotEmpty(fileName)){
+// fileName = zipUrl.substring(zipUrl.lastIndexOf("/")) + fileName.substring(fileName.lastIndexOf("."), fileName.length()-1);
+// zipPath = zipPath.substring(0, zipPath.lastIndexOf("/")+1) + fileName;
+// }
+// try (BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
+// FileOutputStream fileOutputStream = new FileOutputStream(zipPath)) {
+// byte dataBuffer[] = new byte[1024];
+// int bytesRead;
+// while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
+// fileOutputStream.write(dataBuffer, 0, bytesRead);
+// }
+// } catch (IOException e) {
+// log.error("文件下载失败: {}-{}", zipUrl, zipPath);
+// throw e;
+// }
+
+ if(fileName.contains(".zip") || fileName.contains(".rar")){
+ String command1 = "unar " + zipPath + " -o " + zipPath.substring(0, zipPath.lastIndexOf("/")+1) + zipUrl.substring(zipUrl.lastIndexOf("/")+1);
+ String command2 = "rm -rf " + zipPath;
+ log.info("command1:{},command2:{}", command1, command2);
+ String s = RuntimeUtil.execForStr(command1);
+// RuntimeUtil.execForStr(command2);
+// String s = RuntimeUtil.execForStr("ls "+ zipPath.substring(0, zipPath.lastIndexOf("/")+1));
+ System.out.println(s);
+
+// log.info("result1:{}, result2:{}", result, result2);
}
}
diff --git a/src/main/java/net/educoder/ecsonar/utils/SslUtils.java b/src/main/java/net/educoder/ecsonar/utils/SslUtils.java
new file mode 100644
index 0000000..297d504
--- /dev/null
+++ b/src/main/java/net/educoder/ecsonar/utils/SslUtils.java
@@ -0,0 +1,67 @@
+package net.educoder.ecsonar.utils;
+
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSession;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+
+/**
+ * @Author: youys
+ * @Date: 2022/5/30
+ * @Description:
+ */
+
+public class SslUtils {
+
+ private static void trustAllHttpsCertificates() throws Exception {
+ TrustManager[] trustAllCerts = new TrustManager[1];
+ TrustManager tm = new miTM();
+ trustAllCerts[0] = tm;
+ SSLContext sc = SSLContext.getInstance("SSL");
+ sc.init(null, trustAllCerts, null);
+ HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
+ }
+
+ static class miTM implements TrustManager,X509TrustManager {
+ public X509Certificate[] getAcceptedIssuers() {
+ return null;
+ }
+
+ public boolean isServerTrusted(X509Certificate[] certs) {
+ return true;
+ }
+
+ public boolean isClientTrusted(X509Certificate[] certs) {
+ return true;
+ }
+
+ public void checkServerTrusted(X509Certificate[] certs, String authType)
+ throws CertificateException {
+ return;
+ }
+
+ public void checkClientTrusted(X509Certificate[] certs, String authType)
+ throws CertificateException {
+ return;
+ }
+ }
+
+ /**
+ * 忽略HTTPS请求的SSL证书,必须在openConnection之前调用
+ * @throws Exception
+ */
+ public static void ignoreSsl() throws Exception{
+ HostnameVerifier hv = new HostnameVerifier() {
+ public boolean verify(String urlHostName, SSLSession session) {
+ return true;
+ }
+ };
+ trustAllHttpsCertificates();
+ HttpsURLConnection.setDefaultHostnameVerifier(hv);
+ }
+}
\ No newline at end of file
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 8f9fc25..f933f5d 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -43,7 +43,7 @@ zip.save.path=/tmp/
#excel.template.path=/Users/guange/work/java/ecsonar/src/main/resources/template.xlsx
excel.template.path=template.xlsx
-sonar.url=http://127.0.0.1:9000
+sonar.url=https://sonar.educoder.net
#sonar.url=http://117.50.14.123:9000
# token令牌
sonar.token=0253a518e824a976ea2f11aec17938cb0f8c0495
diff --git a/src/test/java/net/educoder/ecsonar/EcsonarApplicationTests.java b/src/test/java/net/educoder/ecsonar/EcsonarApplicationTests.java
index dd4056a..aeb0698 100644
--- a/src/test/java/net/educoder/ecsonar/EcsonarApplicationTests.java
+++ b/src/test/java/net/educoder/ecsonar/EcsonarApplicationTests.java
@@ -15,6 +15,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
@@ -67,8 +68,8 @@ public class EcsonarApplicationTests {
public void scanExcel(){
CountDownLatch countDownLatch = new CountDownLatch(1);
- String excelPath = "/Users/youyongsheng/Desktop/res.xls";
-// String excelPath = "/Users/youyongsheng/Desktop/res-test.xlsx";
+ String excelPath = "/Users/youyongsheng/Desktop/智擎/质量分析/2022-05-30/res.xls";
+// String excelPath = "/Users/youyongsheng/Desktop/111111.xlsx";
sonarService.scanExcel(excelPath);
try {
@@ -81,13 +82,17 @@ public class EcsonarApplicationTests {
@Test
public void writeExcel() throws Exception {
- int homeworkId = 20210101;
- String directory = "/tmp/20210101/";
+ int homeworkId = 557155;
+ String directory = "/tmp/557155/";
File file = new File(directory);
File[] files = file.listFiles();
System.out.println("学生数:" + files.length);
- List personList = ExcelUtil.readExcel("/Users/youyongsheng/Desktop/res.xls");
+ List personList = ExcelUtil.readExcel("/Users/youyongsheng/Desktop/智擎/质量分析/2022-05-30/res.xls");
+// List personList = new ArrayList<>();
+// Person person = new Person();
+// person.setUid("201805550117");
+// person.setUid("201805550117");
Map> collect = personList.stream().collect(Collectors.groupingBy(Person::getUid));
@@ -100,7 +105,7 @@ public class EcsonarApplicationTests {
Metrics metrics = reportService.getMetrics(projectName);
String templatePath = this.getClass().getClassLoader().getResource("template1.xlsx").getPath();
- String outPath = "/Users/youyongsheng/Desktop/202100000004.xlsx";
+ String outPath = "/Users/youyongsheng/Desktop/2022-05-30-111.xlsx";
System.out.println("第"+(i++)+"个学生," + uid);
if (CollectionUtils.isEmpty(collect.get(uid))) {
@@ -121,5 +126,20 @@ public class EcsonarApplicationTests {
System.out.println("---------end------------");
+
+ }
+
+ @Test
+ public void writeOne() throws Exception {
+ String templatePath = this.getClass().getClassLoader().getResource("template1.xlsx").getPath();
+ String outPath = "/Users/youyongsheng/Desktop/2022-05-18-tlj.xlsx";
+
+ Metrics metrics = reportService.getMetrics("20220520-tanglijin");
+ reportService.writeSpecifyTemplateToExcel("201805550117","唐立锦"
+ ,
+ metrics,
+ templatePath,
+ outPath);
+
}
}