小编TDG*_*TDG的帖子

我在Jsoup中得到一个SocketTimeoutException:读取超时


当我尝试使用Jsoup解析大量HTML文档时,我得到一个SocketTimeoutException.
例如,我有一个链接列表:

<a href="www.domain.com/url1.html">link1</a>
<a href="www.domain.com/url2.html">link2</a>
<a href="www.domain.com/url3.html">link3</a>
<a href="www.domain.com/url4.html">link4</a>
Run Code Online (Sandbox Code Playgroud)

对于每个链接,我解析链接到URL的文档(来自href属性)以获取这些页面中的其他信息.
所以我可以想象它需要很多时间,但是如何关闭这个例外呢?
这是整个堆栈跟踪:

java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.io.BufferedInputStream.fill(Unknown Source)
    at java.io.BufferedInputStream.read1(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
    at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:381)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:364)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:143)
    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:132)
    at app.ForumCrawler.crawl(ForumCrawler.java:50)
    at Main.main(Main.java:15)
Run Code Online (Sandbox Code Playgroud)

谢谢你的哥们!

编辑: 哼......抱歉,刚刚找到了解决方案:

Jsoup.connect(url).timeout(0).get();
Run Code Online (Sandbox Code Playgroud)

希望对别人有用...... :)

java jsoup

99
推荐指数
2
解决办法
6万
查看次数

Java图形随机文本

我正在尝试使用随机 x,y 值fontSize根据 BufferedImage 上的词频绘制随机文本。drawString(word, x, y)不幸的是,它绘制了重叠的随机文本。图像帧大小为 1200 x 650,x、y 的随机数位于这些值之间。这是我的代码:

Random rand = new Random();
Font f = getRandomFont(fontSize);
FontMetrics metrics = graphics.getFontMetrics(f);
AffineTransform affinetransform = new AffineTransform();     
FontRenderContext frc = new FontRenderContext(affinetransform,true,true);
int textwidth = (int)(f.getStringBounds(word, frc).getWidth());
int textheight = (int)(f.getStringBounds(word, frc).getHeight());
graphics.setColor(Color.red);
graphics.setFont(f);
int x = textwidth + rand.nextInt(800);
int y = -textheight + rand.nextInt(800);
graphics.drawString(word, x , y );
Run Code Online (Sandbox Code Playgroud)

java random

3
推荐指数
1
解决办法
940
查看次数

标签 统计

java ×2

jsoup ×1

random ×1