一、NewURL概述
public class NewURL { private String protocol; private String hostname; private int port; private String path; private String query; private String fragment; public NewURL(String spec) throws MalformedURLException { this((URL)null, spec); } public NewURL(URL context, String spec) throws MalformedURLException { String original = spec; int i, limit, c; int start = 0; String newProtocol = null; boolean aRef = false; try { limit = spec.length(); while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) { limit--; } while ((start < limit) && (spec.charAt(start) <= ' ')) { start++; } if (spec.regionMatches(true, start, "url:", 0, 4)) { start += 4; } if (start < spec.length() && spec.charAt(start) == '#') { /* we're assuming this is a ref relative to the context */ aRef = true; } for (i = start ; !aRef && (i < limit) && ((c = spec.charAt(i)) != ':') ; i++) { if (c == '/') { break; } } /* Collect the protocol (if any) */ if (i < limit && spec.charAt(i) == ':') { newProtocol = spec.substring(start, i); start = i + 1; } /* Get the host and port */ if (newProtocol != null) { while (start < limit) { c = spec.charAt(start); if (c == '/' || c == '\\' || c == '?' || c == '#') { break; } start++; } String authority = spec.substring(i + 1, start); AuthorityParser ap = new AuthorityParser(authority); this.hostname = ap.hostname; this.port = ap.port; } else { /* Determine if this is a scheme-less URL with an authority */ this.hostname = ""; this.port = -1; int at = spec.indexOf('@', start); if (at != -1) { int colon = spec.indexOf(':', at + 1); int i2 = spec.lastIndexOf('/', at); if (i2 == -1) { i2 = start; } else if (i2 -1 && colon < i2) { /* Couldn't be sure it's not a port */ } else { this.hostname = spec.substring(at + 1, i2); start = i2; } } } /* Get the path */ if (start < limit) { this.path = spec.substring(start, limit); } else { this.path = ""; } /* * Compute hashCode and String representation as soon as possible * to maximize the performance and minimize the risk of locking * in the URLStreamHandler. */ this.hash = hashCode(); } catch (MalformedURLException e) { throw e; } catch (Exception e) { throw new MalformedURLException(e.getMessage()); } } }
Java中的NewURL類是一個用於生成或者解析URL的庫,可以方便地生成HTTP及HTTPS的URL對象,並且支持各種URL的操作。在構造方法中傳入URL所需要的各項參數,可以使用它來獲取主機名、端口號、路徑、查詢參數和錨點信息等。
二、NewURL實現步驟
1. 設置屬性
在創建NewURL實例時,需要先設置各個屬性。這些屬性包括URL協議、主機名、端口號、路徑、查詢參數和錨點信息。
private String protocol; private String hostname; private int port; private String path; private String query; private String fragment;
2. 解析URL字符串
在創建NewURL實例時,需要解析一個URL字符串,並生成對應的URL對象。NewURL類提供了兩個構造方法:帶參構造方法和無參構造方法。
public NewURL(String spec) throws MalformedURLException; public NewURL(URL context, String spec) throws MalformedURLException;
其中,無參的構造方法生成一個默認的URL對象,而有參的構造方法則需要傳入一個URL字符串或者另外一個URL對象來生成一個新的URL對象。
3. URL字符串分析方法
在解析URL字符串時,可以使用以下方法獲取URL各個部分的信息:
/** * 從協議字符串返回對應協議名稱。 * * @return 協議名稱或null對於URL沒有指定協議。 */ public String getProtocol(); /** * 從主機名字符串返回對應的主機名。 * * @return 主機名或null,如果沒有指定主機名 */ public String getHost(); /** * 從端口號返回此URL的端口號,如果未指定則返回-1。 * * @return 端口號或-1,如果沒有指定端口號 */ public int getPort(); /** * 返回此URL的文件名部分。 * * @return 文件名部分或null,如果未指定文件名部分 */ public String getPath(); /** * 查詢字符串。 * * @return 查詢字符串或null,如果未指定查詢字符串 * * @see #getQuery(Map) */ public String getQuery(); /** * 返回此URL的參考。 * * @return 參考或null,如果未指定 */ public String getRef();
三、NewURL使用示例
下面是一個使用NewURL生成HTTP的示例:
public class HTTPTest { public static void main(String[] args) throws Exception { NewURL url = new NewURL("http://www.example.com:8080/path/resource.html?param1=test¶m2=123#anchor"); String protocol = url.getProtocol(); String host = url.getHost(); int port = url.getPort(); String path = url.getPath(); String query = url.getQuery(); String fragment = url.getRef(); System.out.println("Protocol: " + protocol); System.out.println("Host: " + host); System.out.println("Port: " + port); System.out.println("Path: " + path); System.out.println("Query: " + query); System.out.println("Fragment: " + fragment); } }
輸出結果為:
Protocol: http Host: www.example.com Port: 8080 Path: /path/resource.html Query: param1=test¶m2=123 Fragment: anchor
可以看到,NewURL類非常方便地實現了對HTTP URL的解析和生成。在開發Web應用程序時,這個類是非常實用的。
原創文章,作者:LKWJ,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/145043.html