一、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/n/145043.html