|  |  | @ -1,7 +1,15 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | package net.educoder.ecsonar.task; |  |  |  | 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.model.api.SonarRequest; | 
			
		
	
		
		
			
				
					
					|  |  |  | import net.educoder.ecsonar.services.SonarService; |  |  |  | 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.apache.commons.lang3.StringUtils; | 
			
		
	
		
		
			
				
					
					|  |  |  | import org.slf4j.Logger; |  |  |  | import org.slf4j.Logger; | 
			
		
	
		
		
			
				
					
					|  |  |  | import org.slf4j.LoggerFactory; |  |  |  | import org.slf4j.LoggerFactory; | 
			
		
	
	
		
		
			
				
					|  |  | @ -10,8 +18,11 @@ import java.io.BufferedInputStream; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.File; |  |  |  | import java.io.File; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.FileOutputStream; |  |  |  | import java.io.FileOutputStream; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.IOException; |  |  |  | import java.io.IOException; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import java.net.HttpURLConnection; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.net.URL; |  |  |  | import java.net.URL; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.net.URLConnection; |  |  |  | import java.net.URLConnection; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import java.net.URLDecoder; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | /** |  |  |  | /** | 
			
		
	
		
		
			
				
					
					|  |  |  |  * @Author: youys |  |  |  |  * @Author: youys | 
			
		
	
	
		
		
			
				
					|  |  | @ -39,15 +50,15 @@ public class ReadExcelRunnable implements Runnable{ | 
			
		
	
		
		
			
				
					
					|  |  |  |         String path = String.format("/tmp/%d/%s/", homeworkId, uid); |  |  |  |         String path = String.format("/tmp/%d/%s/", homeworkId, uid); | 
			
		
	
		
		
			
				
					
					|  |  |  |         File file = new File(path); |  |  |  |         File file = new File(path); | 
			
		
	
		
		
			
				
					
					|  |  |  |         if(!file.exists()){ |  |  |  |         if(!file.exists()){ | 
			
		
	
		
		
			
				
					
					|  |  |  |             file.mkdir(); |  |  |  |             file.mkdirs(); | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | //        for (Person person : sonarRequest.getPersonList()) {
 |  |  |  | //        for (Person person : sonarRequest.getPersonList()) {
 | 
			
		
	
		
		
			
				
					
					|  |  |  | //            String[] split = StringUtils.split(person.getDownloadUrl(), "/");
 |  |  |  | //            String[] split = StringUtils.split(person.getDownloadUrl(), "/");
 | 
			
		
	
		
		
			
				
					
					|  |  |  | //            String zipFilename = UrlUtil.getURLDecoderString(split[split.length - 1]);
 |  |  |  | //            String zipFilename = UrlUtil.getURLDecoderString(split[split.length - 1]);
 | 
			
		
	
		
		
			
				
					
					|  |  |  | //            try {
 |  |  |  | //            try {
 | 
			
		
	
		
		
			
				
					
					|  |  |  | //                download(person.getDownloadUrl(),path+zipFilename);
 |  |  |  | //                download(person.getDownloadUrl(),path+zipFilename);
 | 
			
		
	
		
		
			
				
					
					|  |  |  | //            } catch (IOException e) {
 |  |  |  | //            } catch (Exception e) {
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | //                e.printStackTrace();
 |  |  |  | //                log.error("download exception", e);
 | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  | //            }
 |  |  |  | //            }
 | 
			
		
	
		
		
			
				
					
					|  |  |  | //        }
 |  |  |  | //        }
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         String key = String.format("%d-%s", homeworkId, uid); |  |  |  |         String key = String.format("%d-%s", homeworkId, uid); | 
			
		
	
	
		
		
			
				
					|  |  | @ -55,11 +66,13 @@ public class ReadExcelRunnable implements Runnable{ | 
			
		
	
		
		
			
				
					
					|  |  |  |         sonarService.sonar(path,key); |  |  |  |         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); |  |  |  |         log.info("下载文件: {}-{}", zipUrl, zipPath); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         String cookie = "_educoder_session=1f4bf34409fec2180b99aa1cbf0b7586"; |  |  |  | 
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         String cookie = "_educoder_session=2237164bc8ff253aa43c8e089015da7a"; | 
			
		
	
		
		
			
				
					
					|  |  |  |         URL url = new URL(zipUrl); |  |  |  |         URL url = new URL(zipUrl); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         SslUtils.ignoreSsl(); | 
			
		
	
		
		
			
				
					
					|  |  |  |         URLConnection conn = url.openConnection(); |  |  |  |         URLConnection conn = url.openConnection(); | 
			
		
	
		
		
			
				
					
					|  |  |  |         conn.setRequestProperty("Cookie", cookie); |  |  |  |         conn.setRequestProperty("Cookie", cookie); | 
			
		
	
		
		
			
				
					
					|  |  |  |         conn.setDoInput(true); |  |  |  |         conn.setDoInput(true); | 
			
		
	
	
		
		
			
				
					|  |  | @ -67,19 +80,37 @@ public class ReadExcelRunnable implements Runnable{ | 
			
		
	
		
		
			
				
					
					|  |  |  |         String fileName = conn.getHeaderField("Content-Disposition"); |  |  |  |         String fileName = conn.getHeaderField("Content-Disposition"); | 
			
		
	
		
		
			
				
					
					|  |  |  |         if(StringUtils.isNotEmpty(fileName)){ |  |  |  |         if(StringUtils.isNotEmpty(fileName)){ | 
			
		
	
		
		
			
				
					
					|  |  |  |             fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8"); |  |  |  |             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); |  |  |  |             fileName = fileName.substring(fileName.indexOf("filename=")+10,fileName.length()-1); | 
			
		
	
		
		
			
				
					
					|  |  |  |             zipPath = zipPath.substring(0, zipPath.lastIndexOf("/")+1) + fileName; |  |  |  |             zipPath = zipPath.substring(0, zipPath.lastIndexOf("/")+1) + fileName; | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |         try (BufferedInputStream in = new BufferedInputStream(conn.getInputStream()); |  |  |  | 
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |              FileOutputStream fileOutputStream = new FileOutputStream(zipPath)) { |  |  |  | //        if(StringUtils.isNotEmpty(fileName)){
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             byte dataBuffer[] = new byte[1024]; |  |  |  | //            fileName = zipUrl.substring(zipUrl.lastIndexOf("/")) + fileName.substring(fileName.lastIndexOf("."), fileName.length()-1);
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             int bytesRead; |  |  |  | //            zipPath = zipPath.substring(0, zipPath.lastIndexOf("/")+1) + fileName;
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { |  |  |  | //        }
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 fileOutputStream.write(dataBuffer, 0, bytesRead); |  |  |  | //        try (BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  | //             FileOutputStream fileOutputStream = new FileOutputStream(zipPath)) {
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         } catch (IOException e) { |  |  |  | //            byte dataBuffer[] = new byte[1024];
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             log.error("文件下载失败: {}-{}", zipUrl, zipPath); |  |  |  | //            int bytesRead;
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             throw e; |  |  |  | //            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);
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
	
		
		
			
				
					|  |  | 
 |