Java獲取URL對應的資源
認識IP、認識URL是進行網絡編程的第一步。java.net.URL提供了豐富的URL構建方式,並可以通過java.net.URL來獲取資源。
一、認識URL
類
URL
代表一個統一資源定位符,它是指向互聯網“資源”的指針。資源可以是簡單的文件或目錄,也可以是對更為復雜的對象的引用,例如對數據庫或搜索引擎的查詢。
簡單的可以把URL理解為包含:協議、主機名、端口、路徑、查詢字符串和參數等對象。每一段可以獨立設置。
應用程序也可以指定一個“相對 URL”,它只包含到達相對於另一個 URL 的資源的足夠信息。HTML 頁面中經常使用相對 URL。
相對 URL 不需要指定 URL 的所有組成部分。如果缺少協議、主機名稱或端口號,這些值將從完整指定的 URL 中繼承。
由於 URL 不懂 URL 轉義,所以它不會識別同一 URL 的對等編碼和解碼形式。
注意,
URI
類在某些特定情況下對其組成字段執行轉義。建議使用
URI
管理 URL 的編碼和解碼,並使用
toURI()
和
URI.toURL()
實現這兩個類之間的轉換。
也可以使用
URLEncoder
和
URLDecoder
類,但是只適用於 HTML 形式的編碼,它與 RFC2396 中定義的編碼機制不同。
(以上介紹來自Java API doc)
二、URL對象的構建
方式很多,可以看看API文檔。
三、獲取URL指定的資源
下面給個例子,說明如何獲取到指定的資源。
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class TestURL {
public static void main(String[] args) throws IOException {
test4();
test3();
test2();
test();
}
/**
* 獲取URL指定的資源。
*
* @throws IOException
*/
public static void test4() throws IOException {
URL url = new URL( "http://lavasoft.blog.51cto.com/attachment/200811/200811271227767778082.jpg");
//獲得此 URL 的內容。
Object obj = url.getContent();
System.out.println(obj.getClass().getName());
}
/**
* 獲取URL指定的資源
*
* @throws IOException
*/
public static void test3() throws IOException {
URL url = new URL("http://www.hrtsea.com/down/soft/45.htm");
//返回一個 URLConnection 對象,它表示到 URL 所引用的遠程對象的連接。
URLConnection uc = url.openConnection();
//打開的連接讀取的輸入流。
InputStream in = uc.getInputStream();
int c;
while ((c = in.read()) != -1)
System.out.print(c);
in.close();
}
/**
* 讀取URL指定的網頁內容
*
* @throws IOException
*/
public static void test2() throws IOException {
URL url = new URL("http://www.hrtsea.com/down/soft/45.htm");
//打開到此 URL 的連接並返回一個用於從該連接讀入的 InputStream。
Reader reader = new InputStreamReader(new BufferedInputStream(url.openStream()));
int c;
while ((c = reader.read()) != -1) {
System.out.print((char) c);
}
reader.close();
}
/**
* 獲取URL的輸入流,並輸出
*
* @throws IOException
*/
public static void test() throws IOException {
URL url = new URL("http://lavasoft.blog.51cto.com/62575/120430");
//打開到此 URL 的連接並返回一個用於從該連接讀入的 InputStream。
InputStream in = url.openStream();
int c;
while ((c = in.read()) != -1)
System.out.print(c);
in.close();
}
}
import java.net.URL;
import java.net.URLConnection;
public class TestURL {
public static void main(String[] args) throws IOException {
test4();
test3();
test2();
test();
}
/**
* 獲取URL指定的資源。
*
* @throws IOException
*/
public static void test4() throws IOException {
URL url = new URL( "http://lavasoft.blog.51cto.com/attachment/200811/200811271227767778082.jpg");
//獲得此 URL 的內容。
Object obj = url.getContent();
System.out.println(obj.getClass().getName());
}
/**
* 獲取URL指定的資源
*
* @throws IOException
*/
public static void test3() throws IOException {
URL url = new URL("http://www.hrtsea.com/down/soft/45.htm");
//返回一個 URLConnection 對象,它表示到 URL 所引用的遠程對象的連接。
URLConnection uc = url.openConnection();
//打開的連接讀取的輸入流。
InputStream in = uc.getInputStream();
int c;
while ((c = in.read()) != -1)
System.out.print(c);
in.close();
}
/**
* 讀取URL指定的網頁內容
*
* @throws IOException
*/
public static void test2() throws IOException {
URL url = new URL("http://www.hrtsea.com/down/soft/45.htm");
//打開到此 URL 的連接並返回一個用於從該連接讀入的 InputStream。
Reader reader = new InputStreamReader(new BufferedInputStream(url.openStream()));
int c;
while ((c = reader.read()) != -1) {
System.out.print((char) c);
}
reader.close();
}
/**
* 獲取URL的輸入流,並輸出
*
* @throws IOException
*/
public static void test() throws IOException {
URL url = new URL("http://lavasoft.blog.51cto.com/62575/120430");
//打開到此 URL 的連接並返回一個用於從該連接讀入的 InputStream。
InputStream in = url.openStream();
int c;
while ((c = in.read()) != -1)
System.out.print(c);
in.close();
}
}
四、Java所支持的URL類型
類型很多,通過下面這個工具可以測試,這個類來自
[url]http://www.java2s.com/Tutorial/Java/0320__Network/ProtocolTester.htm[/url]。
import java.net.URL;
public class MainClass {
public static void main(String[] args) {
String host = "www.java2s.com";
String file = "/index.html";
String[] schemes = { "http", "https", "ftp", "mailto", "telnet", "file", "ldap", "gopher",
"jdbc", "rmi", "jndi", "jar", "doc", "netdoc", "nfs", "verbatim", "finger", "daytime",
"systemresource"};
for ( int i = 0; i < schemes.length; i++) {
try {
URL u = new URL(schemes[i], host, file);
System.out.println(schemes[i] + " is supported\r\n");
} catch (Exception ex) {
System.out.println(schemes[i] + " is not supported\r\n");
}
}
}
}
public class MainClass {
public static void main(String[] args) {
String host = "www.java2s.com";
String file = "/index.html";
String[] schemes = { "http", "https", "ftp", "mailto", "telnet", "file", "ldap", "gopher",
"jdbc", "rmi", "jndi", "jar", "doc", "netdoc", "nfs", "verbatim", "finger", "daytime",
"systemresource"};
for ( int i = 0; i < schemes.length; i++) {
try {
URL u = new URL(schemes[i], host, file);
System.out.println(schemes[i] + " is supported\r\n");
} catch (Exception ex) {
System.out.println(schemes[i] + " is not supported\r\n");
}
}
}
}
另外,還可以通過協議處理器自定義協議。
http://lavasoft.blog.51cto.com/62575/120445/