public final class URLUtil extends Object
URL
工具类.
URL 的最大长度是多少?W3C 的 HTTP 协议 并没有限定; 然而经过试验,不同浏览器和 Web 服务器有不同的约定:
- IE 长度上限是 2083 字节,其中纯路径部分不能超过 2048 字节.
- Firefox 地址栏中超过 65536 字符后就不再显示.
- Safari 测试到 80000 字符还工作得好好的.
- Opera 测试到 190000 字符还工作得好好的.
Web 服务器:
- Apache Web 服务器在接收到大约 4000 字符长的 URL 时候产生 "413 Entity Too Large" 错误.
- IIS 默认接收的最大 URL 是 16384 字符.
Modifier and Type | Method and Description |
---|---|
static String |
getUnionUrl(URL context,
String spec)
获取联合url,通过在指定的上下文中对给定的 spec 进行解析创建 URL,新的 URL 从给定的上下文 URL 和 spec 参数创建.
|
static InputStream |
openStream(URL url)
打开当前
url 的连接,并且读取返回 InputStream . |
static URI |
toURI(URL url)
|
static URL |
toURL(String spec)
将字符串
spec 转成 URL . |
public static URL toURL(String spec)
spec
转成 URL
.
- 处理 check exception,转成 uncheck exception
- 如果使用new URL 出现异常,会尝试使用
toFileURL(String)
如果直接调用
URL.URL(String)
方法,如果是文件路径, 比如String spec = "C:/Users/feilong/feilong/train/新员工/warmReminder/20160704141057.html"; URL newURL = new URL(spec);会抛出异常,Caused by: java.net.MalformedURLException: unknown protocol: c at java.net.URL.此时 ,你需要修改成<init>
(URL.java:593) at java.net.URL.<init>
(URL.java:483) at java.net.URL.<init>
(URL.java:432) ... 24 moreString spec = "file://C:/Users/feilong/feilong/train/新员工/warmReminder/20160704141057.html"; URL newURL = new URL(spec);也就是说,此方法必须要有协议支持 (file URI scheme),如果你是文件需要转成 url的话,建议直接调用toFileURL(String)
spec
- the String
to parse as a URL.spec
是null,抛出 NullPointerException
spec
是blank,抛出 IllegalArgumentException
URL.URL(String)
,
"org.apache.cxf.common.util.StringUtils#getURL(String)",
"org.springframework.util.ResourceUtils#getURL(String)",
File_URI_scheme,
"org.apache.xml.resolver.readers.TextCatalogReader#readCatalog(Catalog, String)",
Creating a URLpublic static URI toURI(URL url)
url
- the urlurl
是null,返回nullURL.toURI()
public static InputStream openStream(URL url)
url
的连接,并且读取返回 InputStream
.
这个方法是以下方法的简写
url.openConnection().getInputStream()
url
- the urlurl
是null,返回 nullURL.openStream()
public static String getUnionUrl(URL context, String spec)
网站地址拼接,请使用这个method
URL url = new URL("http://www.exiaoshuo.com/jinyiyexing/"); URIUtil.getUnionUrl(url, "/jinyiyexing/1173348/") = http://www.exiaoshuo.com/jinyiyexing/1173348/
context
- 要解析规范的上下文spec
- the String
to parse as a URL.spec
是null,抛出 NullPointerException
spec
是blank,抛出 IllegalArgumentException
Copyright © 2008-2019 by feilong