學(xué)了這么多年了裁厅,今天大概了解他倆的區(qū)別:
URI>URL,即URI包括URL,URI的組成部分包括URL和URN
URI:Universal Resource Identifier(統(tǒng)一資源標(biāo)志符),用來(lái)標(biāo)識(shí)抽象或物理資源的一個(gè)緊湊字符串
URL:Universal Resource Locator(統(tǒng)一資源定位符),一種定位資源的主要訪問(wèn)機(jī)制的字符串,一個(gè)標(biāo)準(zhǔn)的URL必須包括:protocol扇调、host、port抢肛、path狼钮、parameter碳柱、anchor。
URN :Universal Resource Name 統(tǒng)一資源名稱熬芜,通過(guò)特定命名空間中的唯一名稱或ID來(lái)標(biāo)識(shí)資源莲镣。
舉個(gè)栗子:
個(gè)人的身份證號(hào)就是URN,個(gè)人的家庭地址就是URL涎拉,URN可以唯一標(biāo)識(shí)一個(gè)人瑞侮,而URL可以告訴郵遞員怎么把貨送到你手里。
再舉個(gè)栗子:
http://blog.csdn.net/koflance是個(gè)URL鼓拧,通過(guò)這個(gè)網(wǎng)址可以告訴CDN找到我的博客所在地半火,并且還告訴用HTTP協(xié)議訪問(wèn),而isbn:0-395-36341-1是RUN季俩,一個(gè)國(guó)際標(biāo)準(zhǔn)書號(hào)钮糖,可以唯一確定哪本書。
目前HTTP規(guī)范已經(jīng)不使用URL酌住,而是使用URI了店归,所以大家還是用URI吧,準(zhǔn)沒錯(cuò)消痛!
HTTP relies upon the Uniform Resource Identifier (URI) standard
[RFC3986] to indicate the target resource (Section 5.1) and
relationships between resources.
Java中的URI和URL組件
URI組件
該類是不可變類(Instances of this class are immutable),提供了用于從其組成部分或通過(guò)解析其字符串形式創(chuàng)建 URI 實(shí)例的構(gòu)造方法都哭、用于訪問(wèn)實(shí)例的各個(gè)不同組成部分的方法,以及用于對(duì) URI 實(shí)例進(jìn)行規(guī)范化质涛、解析和相對(duì)化的方法。
在java的uri類中汇陆,將URI字符串分為絕對(duì)RUI和相對(duì)URI带饱,或者不透明URI(opaque uri)和分層URI(hierarchical uri)。其中勺疼,不透明 URI 為絕對(duì) URI教寂,且不透明的URI無(wú)法解析酪耕,例如:mailto:java-net@java.sun.com 、news:comp.lang.java轨淌、urn:isbn:096139210x看尼,而分層URI即可以為絕對(duì)URI,也可以是相對(duì)URI藏斩,例如http://java.sun.com/j2se/1.3/ 是絕對(duì)URI,而../../../demo/jfc/SwingSet2/src/SwingSet2.java是相對(duì)URI却盘。
分層URI
其結(jié)構(gòu)為:[ scheme :][ // authority][ path][ ? query][ # fragment]
scheme與fragment部分是授權(quán)組成部分狰域,可以基于服務(wù),也可以基于注冊(cè)表黄橘,常見的是基于服務(wù)兆览,格式為:[ user-info @] host[ : port]
在給定實(shí)例中,任何特殊組成部分都或者為未定義的塞关,或者為已定義的抬探,并且有不同的值。未定義的字符串組成部分由 null 表示描孟,未定義的整數(shù)組成部分由 -1 表示驶睦。
常用方法
URI normalize()? ? //規(guī)范化此URI的路徑。(規(guī)范化: 將分層 URI 的路徑組成部分中的不必要的 "." 和 ".." 部分移除的過(guò)程匿醒。每個(gè) "." 部分都將被移除场航。".." 部分也被移除,除非它前面有一個(gè)非 ".." 部分廉羔。規(guī)范化對(duì)不透明 URI 不產(chǎn)生任何效果溉痢。)
URI relativize(URI uri)? ? //根據(jù)此 URI 將給定 URI 相對(duì)化。
URI parseServerAuthority()? ? //嘗試將此 URI 的授權(quán)組成部分(如果已定義)解析為用戶信息憋他、主機(jī)和端口組成部分孩饼。
URI resolve(URI uri)? ? //根據(jù)此 URI 解析給定的 URI。
URI resolve(String str)? ? //解析給定的字符串竹挡,然后在此 URI 的基礎(chǔ)上構(gòu)造一個(gè)新的 URI镀娶。 此方法與進(jìn)行 resolve(URI.create(str)) 的作用相同。
URL組件
提供了解析URL的功能揪罕,可以將URL解析成一個(gè)結(jié)構(gòu)化的數(shù)據(jù)梯码,并提供了簡(jiǎn)單的查找主機(jī)和打開到指定資源的連接之類的網(wǎng)絡(luò) I/O 操作。
第一個(gè)示例:
URL url = new URL("http://www.baidu.com");
//打開到此 URL 的連接并返回一個(gè)用于從該連接讀入的 InputStream好啰。
InputStream in = url.openStream();// 其內(nèi)部也調(diào)用了openConnection()
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
// 讀取內(nèi)容不含響應(yīng)頭
while ((len = in.read(buffer)) != -1)
{
? output.write(buffer, 0, len);
}
System.err.println(new String(output.toByteArray()));
控制臺(tái)打印內(nèi)容:
<!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下轩娶,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a + encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登錄</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多產(chǎn)品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關(guān)于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a> 京ICP證030173號(hào) <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
第二個(gè)示例:
// 方法一
URL url = new URL("http://www.sina.com.cn");
URLConnection urlcon = url.openConnection();
InputStream is = urlcon.getInputStream();
// 方法二
URL url = new URL("http://www.yhfund.com.cn");
HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
InputStream is = urlcon.getInputStream();
//方法三
URL url = new URL("http://www.yhfund.com.cn");
InputStream is = url.openStream();