📄 urlutil.java
字号:
/**
* Created at Nov 21, 2008
*/
package com.jdev.util;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
/**
* <p>Title: URLUtil</p>
* <p>Description: </p>
* @author Lawrence
* @version 1.0
*/
public class URLUtil {
private URLConnection connection = null;
private URL url = null;
private boolean timedOut = false;
/**
* @param url
*/
protected URLUtil(URL url) {
this.url = url;
}
protected URLUtil() {
}
protected synchronized URLConnection openConnection(int timeout)
throws IOException {
Thread t = new Thread(new URLConnectorThread());
t.start();
try {
this.wait(timeout);
} catch (InterruptedException e) {
if (connection == null)
timedOut = true;
else
close(connection);
throw new IOException("Connection never established");
}
if (connection != null) {
return connection;
} else {
timedOut = true;
throw new IOException("Connection timed out");
}
}
public static URLConnection openConnection(URL url) throws IOException {
return openConnection(url, 30000);
}
public static URLConnection openConnection(URL url, int timeout)
throws IOException {
URLUtil uc = new URLUtil(url);
return uc.openConnection(timeout);
}
// special thread to open the connection
private class URLConnectorThread implements Runnable {
public void run() {
URLConnection con = null;
try {
con = url.openConnection();
} catch (IOException e) {
}
synchronized (URLUtil.this) {
if (timedOut && con != null)
close(con);
else {
connection = con;
URLUtil.this.notify();
}
}
}
}
// closes the HttpURLConnection does nothing to others
private static void close(URLConnection con) {
if (con instanceof HttpURLConnection) {
((HttpURLConnection) con).disconnect();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -