Table of Contents
ToggleJava URL Class
網路上資源的引用或位址,透過網路進行通訊,為了更方便識別特定 URI 是否也是 URL,可以檢查其 schema,是否以下任一開頭之一: ftp 、 http 、 https 、 gopher 、 mailto 、 news 、 nntp 、 telnet 、 wais 、 file 或 prospero , URL Class 介紹常見的 toURI 、 sameFile 、 getContent 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- net
| +- url
| +- URLClassTest.java
單元測試
URL Class Java 提供轉換、比較等操作 URL 。
toURI
URL Class Java 建立一個 URL ,轉換成 URI 。
@Test
public void toURI() {
try {
String value = "https://www.ruoxue.org";
URL url = new URL(value);
System.out.println(url);
URI uri = url.toURI();
System.out.println(uri);
assertThat(uri.toString()).isEqualTo(url.toString());
value = "mailto:ruoxueorg@gmail.com";
url = new URL(value);
System.out.println(url);
uri = url.toURI();
System.out.println(uri);
assertThat(uri.toString()).isEqualTo(url.toString());
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
https://www.ruoxue.org
https://www.ruoxue.org
mailto:ruoxueorg@gmail.com
mailto:ruoxueorg@gmail.com
sameFile
URL Class Java 建立兩個 URL ,比較是否相等。
@Test
public void sameFile() {
try {
URL url = new URL("https://www.ruoxue.org/#top");
System.out.println(url);
URL url2 = new URL("https://WWW.RUOXUE.ORG/#bottom");
System.out.println(url2);
boolean result = url.sameFile(url2);
System.out.println(result);
assertThat(result).isTrue();
url = new URL("https://www.ruoxue.org?s=a#bottom");
System.out.println(url);
url2 = new URL("https://www.ruoxue.org?s=%61#top");
System.out.println(url2);
result = url.sameFile(url2);
System.out.println(result);
assertThat(result).isFalse();
} catch (MalformedURLException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
https://www.ruoxue.org/#top
https://WWW.RUOXUE.ORG/#bottom
true
https://www.ruoxue.org?s=a#bottom
https://www.ruoxue.org?s=%61#top
false
getContent
URL Class Java 建立一個 URL ,取得回應內容。
@Test
public void getContent() {
try {
URL url = new URL("https://www.ruoxue.org");
System.out.println(url.getContent());
StringBuilder result = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader((InputStream) url.getContent()))) {
String value;
while ((value = in.readLine()) != null) {
result.append(value);
}
System.out.println(result.length());
assertThat(result).hasSizeGreaterThan(0);
}
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@7cbd213e
20238
URIClassTest.java
URL Class in Java 新增單元測試,驗證 Java URL Class Example 是否符合預期。
package org.ruoxue.java_147.net.url;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import org.junit.Test;
public class URLClassTest {
@Test
public void toURI() {
try {
String value = "https://www.ruoxue.org";
URL url = new URL(value);
System.out.println(url);
URI uri = url.toURI();
System.out.println(uri);
assertThat(uri.toString()).isEqualTo(url.toString());
value = "mailto:ruoxueorg@gmail.com";
url = new URL(value);
System.out.println(url);
uri = url.toURI();
System.out.println(uri);
assertThat(uri.toString()).isEqualTo(url.toString());
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
@Test
public void sameFile() {
try {
URL url = new URL("https://www.ruoxue.org/#top");
System.out.println(url);
URL url2 = new URL("https://WWW.RUOXUE.ORG/#bottom");
System.out.println(url2);
boolean result = url.sameFile(url2);
System.out.println(result);
assertThat(result).isTrue();
url = new URL("https://www.ruoxue.org?s=a#bottom");
System.out.println(url);
url2 = new URL("https://www.ruoxue.org?s=%61#top");
System.out.println(url2);
result = url.sameFile(url2);
System.out.println(result);
assertThat(result).isFalse();
} catch (MalformedURLException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
@Test
public void getContent() {
try {
URL url = new URL("https://www.ruoxue.org");
System.out.println(url.getContent());
StringBuilder result = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader((InputStream) url.getContent()))) {
String value;
while ((value = in.readLine()) != null) {
result.append(value);
}
System.out.println(result.length());
assertThat(result).hasSizeGreaterThan(0);
}
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
}
心得分享
Java URL Class Example 統一資源定位碼,表示 Internet 上某一資源的地址,通過 URL 使用者可以存取各種網路資源,比如常見的 WWW 以及 FTP 站點, URL Class in Java 提供 toURI 、 sameFile 、 getContent 等操作範例。