gpt4 book ai didi

java - jxbrowser中获取异常无法获取浏览器浏览器 channel

转载 作者:行者123 更新时间:2023-12-01 08:48:54 25 4
gpt4 key购买 nike

我正在使用jxbrowser,

我想使用 browser.saveWebpage 下载网址列表

public class Downloader{
public Downloader(String url) {
System.setProperty("teamdev.license.info", "true");
LoggerProvider.getChromiumProcessLogger().setLevel(Level.OFF);
Browser browser = new Browser(BrowserType.LIGHTWEIGHT);

browser.addLoadListener(new LoadAdapter() {
@Override
public void onFinishLoadingFrame(FinishLoadingEvent event) {
if (event.isMainFrame()){
String filePath = "D:\\Downloads\\index"+System.currentTimeMillis()+".html";
String dirPath = "D:\\Downloads\\resources";
event.getBrowser().saveWebPage(filePath, dirPath, SavePageType.ONLY_HTML);
}
}
});
browser.loadURL(url);
if(!browser.isLoading())
{
browser.stop());
}
}


public static void main(String args[])
{
JxBrowserDemo jxBrowserDemo=null;
String yourInputFile="D:/file.txt";
ArrayList<String> lines=getUrls(yourInputFile); //read urls from file

for(int i=0; i<lines.size(); i++)
{
Thread.sleep(5000);
jxBrowserDemo=new JxBrowserDemo(lines.get(i));
}
}

}

保存页面工作正常,直到特定时间,然后抛出此异常

Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa der.java:58) Caused by: com.teamdev.jxbrowser.chromium.internal.ipc.IPCException: Failed to g et Browser browserChannel 969 at com.teamdev.jxbrowser.chromium.Browser.a(SourceFile:376) at com.teamdev.jxbrowser.chromium.Browser.(SourceFile:200) at com.teamdev.jxbrowser.chromium.Browser.(SourceFile:172) at com.teamdev.jxbrowser.chromium.Browser.(SourceFile:139) at com.teamdev.jxbrowser.chromium.Browser.(SourceFile:125) at com.teamdev.jxbrowser.chromium.demo.JxBrowserDemo.(JxBrowserDem o.java:67) at com.teamdev.jxbrowser.chromium.demo.JxBrowserDemo.main(JxBrowserDemo. java:143) ... 5 more

请帮忙:)

最佳答案

“无法获取浏览器 browserChannel 969”消息很可能表明由于某种原因 Chromium 引擎无法创建浏览器实例并为其打开 channel 。Chromium 引擎可能对同时创建的浏览器实例的数量有一些限制。这些限制可能取决于内存大小、环境配置等。由于您已经创建了 968 个浏览器实例而没有处置它们,这可能是此异常的原因。另请注意,您会出现内存泄漏,因为您为每个 URL 创建了新的 Browser 实例,并且在此 URL Browser 实例处理其 URL 后未调用 dispose() 方法。

browser.stop(); 行是不必要的。调用 saveWebPage() 方法后,您应该等待网页保存并释放浏览器实例。请看下面的示例代码:

                final int maxWaitingTime = 10000;
final int sleepTime = 50;
int currentWaitingTime = 0;
File indexHTML = new File(filePath);
File resourcesFolder = new File(dirPath);
while (!indexHTML.exists() || !resourcesFolder.exists()) {
TimeUnit.MILLISECONDS.sleep(sleepTime);
currentWaitingTime += sleepTime;
if (currentWaitingTime == maxWaitingTime)
throw new RuntimeException(new TimeoutException("The web page could not be saved."));
}
event.getBrowser().dispose();

关于java - jxbrowser中获取异常无法获取浏览器浏览器 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42509305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com