2024年10月31日 修改驱动,保持一致性,增加等待时间,升级jar包版本
This commit is contained in:
parent
b1ad133dcf
commit
dd4685c69d
|
@ -88,7 +88,7 @@ public class BlStuAnswerServiceImpl extends ServiceImpl<BlStuAnswerMapper, BlStu
|
|||
//尝试异步加快导出速度
|
||||
log.info("多线程主线程开始执行");
|
||||
//多线程执行
|
||||
CompletionService<Boolean> threadService = ThreadUtil.newCompletionService(Executors.newFixedThreadPool(10));
|
||||
CompletionService<Boolean> threadService = ThreadUtil.newCompletionService(Executors.newFixedThreadPool(100));
|
||||
List<Callable<Boolean>> calls = Lists.newArrayList();
|
||||
List<TableMerge> mergeListT1_1 = Lists.newArrayList();
|
||||
List<TableMerge> mergeListT2_10 = Lists.newArrayList();
|
||||
|
|
|
@ -20,6 +20,7 @@ import org.jeecg.modules.tools.word.WordOperator;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -43,6 +44,9 @@ public class BlTeacherAnswerServiceImpl extends ServiceImpl<BlTeacherAnswerMappe
|
|||
@Autowired
|
||||
public BlTeacherInfoMapper blTeacherInfoMapper;
|
||||
|
||||
//@Resource
|
||||
//public CompletionService<Boolean> threadPoolTaskExecutor;
|
||||
|
||||
@Override
|
||||
public WordOperator createWordTjfx(String id) throws Exception {
|
||||
String templateName = "exp1\\teacher-tjfx.docx";
|
||||
|
@ -70,11 +74,11 @@ public class BlTeacherAnswerServiceImpl extends ServiceImpl<BlTeacherAnswerMappe
|
|||
//尝试异步加快导出速度
|
||||
log.info("多线程主线程开始执行");
|
||||
//多线程执行
|
||||
CompletionService<Boolean> threadService = ThreadUtil.newCompletionService(Executors.newFixedThreadPool(10));
|
||||
List<Callable<Boolean>> calls = com.google.common.collect.Lists.newArrayList();
|
||||
List<TableMerge> allMergeList1 = org.apache.commons.compress.utils.Lists.newArrayList();
|
||||
List<TableMerge> allMergeList2 = org.apache.commons.compress.utils.Lists.newArrayList();
|
||||
List<TableMerge> allMergeList3 = org.apache.commons.compress.utils.Lists.newArrayList();
|
||||
CompletionService<Boolean> threadService = ThreadUtil.newCompletionService(Executors.newFixedThreadPool(100));
|
||||
List<Callable<Boolean>> calls = Lists.newArrayList();
|
||||
List<TableMerge> allMergeList1 = Lists.newArrayList();
|
||||
List<TableMerge> allMergeList2 = Lists.newArrayList();
|
||||
List<TableMerge> allMergeList3 = Lists.newArrayList();
|
||||
|
||||
calls.add(() -> {
|
||||
res1(blTeacherMain, result);
|
||||
|
|
|
@ -11,8 +11,12 @@ import org.apache.commons.lang3.StringUtils;
|
|||
import org.jeecg.common.util.DateUtils;
|
||||
import org.jeecg.common.util.SpringContextHolder;
|
||||
import org.openqa.selenium.JavascriptExecutor;
|
||||
import org.openqa.selenium.SessionNotCreatedException;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WindowType;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
import org.openqa.selenium.chrome.ChromeDriverLogLevel;
|
||||
import org.openqa.selenium.chrome.ChromeDriverService;
|
||||
import org.openqa.selenium.chrome.ChromeOptions;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
@ -20,8 +24,6 @@ import java.awt.image.BufferedImage;
|
|||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Date;
|
||||
|
@ -33,6 +35,11 @@ public class WebDriverUtil {
|
|||
// private static final Global global = null;
|
||||
private static final Global global = SpringContextHolder.getBean(Global.class);
|
||||
|
||||
//驱动
|
||||
// private static WebDriver driver;
|
||||
|
||||
private static ChromeDriverService driverService;
|
||||
|
||||
//private static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
|
||||
|
||||
public static void main(String []s){
|
||||
|
@ -40,9 +47,61 @@ public class WebDriverUtil {
|
|||
// log.info("================================");
|
||||
// log.info(imgName);
|
||||
// log.info("================================");
|
||||
String currentDir = System.getProperty("user.dir");
|
||||
log.info(currentDir);
|
||||
// String currentDir = System.getProperty("user.dir");
|
||||
// log.info(currentDir);
|
||||
// if(driver == null) {
|
||||
WebDriver driver = null;
|
||||
System.setProperty("webdriver.chrome.driver", getUrlStr("web/webDriver/130.0.6723.70/chromedriver.exe"));
|
||||
// System.setProperty("webdriver.chrome.driver", "D:/developkit/driver/chromedriver.exe");
|
||||
//设置 chrome 的无头模式
|
||||
ChromeOptions chromeOptions = new ChromeOptions();
|
||||
//js执行超时时间
|
||||
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
|
||||
chromeOptions.setScriptTimeout(duration);
|
||||
|
||||
//页面加载超时时间
|
||||
chromeOptions.setPageLoadTimeout(duration);
|
||||
//无头模式
|
||||
chromeOptions.setHeadless(false);
|
||||
//地址出现data:,
|
||||
// chromeOptions.addArguments("--user-data-dir=C:/Users/Administrator/AppData/Local/Google/Chrome/User Data/Default");
|
||||
//Chrome正在受到自动软件的控制 不显示提示语
|
||||
chromeOptions.addArguments("disable-infobars");
|
||||
chromeOptions.addArguments("--remote-allow-origins=*");
|
||||
//设置浏览器完整路径
|
||||
chromeOptions.setBinary("D:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
|
||||
//启动一个 chrome 实例
|
||||
driver = new ChromeDriver(chromeOptions);
|
||||
// }
|
||||
|
||||
driver.get("https://www.baidu.com/");
|
||||
|
||||
|
||||
|
||||
WebDriver driver2 = driver.switchTo().newWindow(WindowType.TAB);
|
||||
|
||||
driver2.get("https://cn.bing.com/");
|
||||
|
||||
log.info("第一个页面:{}", driver.getTitle());
|
||||
log.info("第一个页面:{}", driver2.getTitle());
|
||||
|
||||
|
||||
driver.close();
|
||||
driver2.close();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void createService() throws IOException {
|
||||
Duration duration = Duration.of(500, ChronoUnit.SECONDS);
|
||||
ChromeDriverService service = new ChromeDriverService.Builder()
|
||||
//超时时间
|
||||
.withTimeout(duration)
|
||||
//执行驱动
|
||||
//.usingDriverExecutable(getUrl(global.getConfig("webDriver.driverPath")))
|
||||
.build();
|
||||
// service.start();
|
||||
driverService = service;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -50,7 +109,7 @@ public class WebDriverUtil {
|
|||
*
|
||||
* @return
|
||||
*/
|
||||
public static WebDriver getWebDriver() {
|
||||
public static WebDriver getWebDriver() throws IOException {
|
||||
// 设置ChromeDriver的路径加载驱动
|
||||
//"D:/APPLICATION/chromedriver/chromedriver.exe"
|
||||
System.setProperty(global.getConfig("webDriver.driverName"), getUrlStr(global.getConfig("webDriver.driverPath")));
|
||||
|
@ -58,11 +117,12 @@ public class WebDriverUtil {
|
|||
//设置 chrome 的无头模式
|
||||
ChromeOptions chromeOptions = new ChromeOptions();
|
||||
//js执行超时时间
|
||||
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
|
||||
Duration duration = Duration.of(500, ChronoUnit.SECONDS);
|
||||
chromeOptions.setScriptTimeout(duration);
|
||||
|
||||
//页面加载超时时间
|
||||
chromeOptions.setPageLoadTimeout(duration);
|
||||
chromeOptions.setImplicitWaitTimeout(duration);
|
||||
//无头模式
|
||||
chromeOptions.setHeadless(Boolean.parseBoolean(global.getConfig("webDriver.isShow")));
|
||||
//地址出现data:,
|
||||
|
@ -72,8 +132,22 @@ public class WebDriverUtil {
|
|||
chromeOptions.addArguments("--remote-allow-origins=*");
|
||||
//设置浏览器完整路径
|
||||
chromeOptions.setBinary(global.getConfig("webDriver.webExamplePath"));
|
||||
|
||||
chromeOptions.setLogLevel(ChromeDriverLogLevel.ALL);
|
||||
|
||||
//启动一个 chrome 实例
|
||||
return new ChromeDriver(chromeOptions);
|
||||
// if(driverService == null) {
|
||||
// createService();
|
||||
// }
|
||||
ChromeDriverService service = new ChromeDriverService.Builder()
|
||||
//超时时间
|
||||
.withTimeout(duration)
|
||||
//执行驱动
|
||||
//.usingDriverExecutable(getUrl(global.getConfig("webDriver.driverPath")))
|
||||
.build();
|
||||
// service.start();
|
||||
|
||||
return new ChromeDriver(service, chromeOptions);
|
||||
}
|
||||
|
||||
public static File getResourcesFileToTempFile(String path) {
|
||||
|
@ -110,36 +184,50 @@ public class WebDriverUtil {
|
|||
return getImgByte(json, path, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图片byte
|
||||
*/
|
||||
public static String getImgByte(String json, String path, Map<String, String> runJs) {
|
||||
log.info("发往浏览器的json: {}",json);
|
||||
WebDriver driver = null;
|
||||
try {
|
||||
driver = getWebDriver();
|
||||
} catch (SessionNotCreatedException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
log.error("重新整一次????");
|
||||
//不知为何这里总会报连接不上的错误,解决办法就是不停的执行,错一次,多执行一次,但是有问题,就是会多出一个无主的chromedriver,无法关闭
|
||||
//ThreadUtil.sleep(1000);//延迟1秒试试
|
||||
return getImgByte(json, path, runJs);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return getImgByte(driver, json, path, runJs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图片byte
|
||||
*/
|
||||
public static String getImgByte(WebDriver driver, String json, String path, Map<String, String> runJs) {
|
||||
log.info("发往浏览器的json: {}",json);
|
||||
try {
|
||||
//访问网址
|
||||
//往缓存写入jar包里的资源
|
||||
//把js拉过来
|
||||
getUrl(global.getConfig("webDriver.webHTMLPath") + "/echarts.min.js");
|
||||
if(StringUtils.isNotBlank(path)){
|
||||
if (StringUtils.isNotBlank(path)) {
|
||||
driver.get(getUrl(global.getConfig("webDriver.webHTMLPath") + "/" + path).toString());
|
||||
} else {
|
||||
driver.get(getUrl(global.getConfig("webDriver.webHTMLPath") + "/echarts.html").toString());
|
||||
}
|
||||
log.info("最终浏览器执行的地址: {}",driver.getCurrentUrl());
|
||||
log.info("最终浏览器执行的地址: {}", driver.getCurrentUrl());
|
||||
JavascriptExecutor js = (JavascriptExecutor) driver;
|
||||
//执行绘制图片前
|
||||
var runJsStr = "showImg(" + json + ")";
|
||||
//绘制方法是默认的还是自定义的
|
||||
if(runJs != null && runJs.containsKey("showImg")){
|
||||
if (runJs != null && runJs.containsKey("showImg")) {
|
||||
runJsStr = runJs.get("showImg");
|
||||
}
|
||||
if(runJs != null && runJs.containsKey("runBefore")){
|
||||
if (runJs != null && runJs.containsKey("runBefore")) {
|
||||
runJsStr = runJs.get("runBefore") + runJsStr;
|
||||
}
|
||||
//执行绘制图片后
|
||||
if(runJs != null && runJs.containsKey("runAfter")){
|
||||
if (runJs != null && runJs.containsKey("runAfter")) {
|
||||
runJsStr = runJsStr + runJs.get("runAfter");
|
||||
}
|
||||
// 绘制方法是默认的还是自定义的
|
||||
|
@ -153,8 +241,8 @@ public class WebDriverUtil {
|
|||
Thread.sleep(1000);
|
||||
String imgTxt = js.executeScript("return returnEchartImg()").toString().replace("data:image/png;base64,", "");
|
||||
String uid = RandomUtil.randomString(6);
|
||||
String imageName = DateUtils.formatDate(new Date(),"yyyyMMddHHmmss") + uid + ".png";
|
||||
return base64StringToImage(imgTxt,imageName);
|
||||
String imageName = DateUtils.formatDate(new Date(), "yyyyMMddHHmmss") + uid + ".png";
|
||||
return base64StringToImage(imgTxt, imageName);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
|
|
|
@ -164,7 +164,7 @@
|
|||
<dependency>
|
||||
<groupId>org.seleniumhq.selenium</groupId>
|
||||
<artifactId>selenium-java</artifactId>
|
||||
<version>4.21.0</version>
|
||||
<version>4.25.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue