引言:
URL和URI
URI
URI:统一资源标识符(Uniform Resource Identifier) ,URI只是一个纯粹的语法结构,唯一的功能就是解析 。URL是URI的一个特例
URI的组成部分 URI包含用来指定Web资源的字符串的各种组成成分主要由三大部分构成
1 2 3 [scheme:]schemeSpecificPart[#fragment] [方案/协议:]方案具体部分[#片段] <!-- [...]中为可选 -->
包含scheme:
的URI称为绝对URI,否则称为相对URI
schemeSpecificPart
不以/
开头称不透明 的的
所有的绝对的透明URI(第一个)和所有相对URI(第二个)都是分层的
1 2 http://horstmann.com/index.html ../../java/net/Socket.html#Socket()
一个分层的URI的schemeSpecificPart
包含以下结构
1 2 [//authority][path][?query] [//权威][路径][?查询]
而基于服务器的URI,authority
又有以下形式
port
必须是一个整数
所以URI我们可以用以下的方法来读取他们
1 2 3 4 5 6 7 8 9 getScheme() getSchemeSpecificPart() getAuthority() getUserInfo() getHost() getPort() getPath() getQuery() getFragment()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 URI uriAbs = new URI("http://docs.mycompany.com/api/java/net/ServerSocker.html" ); URI uriRel = new URI("../../java/net/Socket.html#Socket()" ); uriAbs.isAbsolute(); uriRel.isAbsolute(); uriAbs.getScheme(); uriAbs.getSchemeSpecificPart(); { uriAbs.getAuthority(); { uriAbs.getUserInfo(); uriAbs.getHost(); uriAbs.getPort(); } uriAbs.getPath(); uriAbs.getQuery(); } uriAbs.getFragment(); uriRel.getFragment();
URI类的作用
处理绝对标识符和相对标识符(是否包含[scheme:])
例如:如果存在以下的一个绝对URI,和相对URI,我们可以组合出一个绝对URI
1 2 3 4 5 6 7 8 绝对URI: http://docs.mycompany.com/api/java/net/ServerSocker.html 相对URI: ../../java/net/Socket.html#Socket() 组合后的绝对URI: http://docs.mycompany.com/api/java/net/ServerSocker.html#Socket()
这个过程叫做解析相对URI ,与此过程相反的过程叫相对化
1 2 3 4 5 6 7 8 基本URI: http://docs.mycompany.com/api 另一个URI: http://docs.mycompany.com/api/java/lang/String.html 相对化后的URI: java/lang/String.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 URI uriAbs = new URI("http://docs.mycompany.com/api/java/net/ServerSocker.html" ); URI uriRel = new URI("../../java/net/Socket.html#Socket()" ); URI uriBase = new URI("http://docs.mycompany.com/api" ); URI uriOther = new URI("http://docs.mycompany.com/api/java/lang/String.html" ); uriBase.relativize(uriOther);
URL
URL:统一资源定位符 (Uniform Resource Locator),URL可以打开一个到达资源的流,URL类只能作用于那些Java类库知道如何处理的类库,如http:、https:、ftp:、本地文件系统file:、JAR文件jar:
包路径
构造方法
常用方法 1 2 public InputStream openStream ()
URLConnection URLConnection类
比URL类
有更多的控制
包路径
常用方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 void setDoInput (boolean doInput) boolean getDoInput () void setDoOutput (boolean doOutput) boolean getDoOutput () void setIfModifiedSince (long time) long getIfModifiedSince () void setUseCaches (boolean useCaches) boolean getUseCaches () void setAllowUserInteraction (boolean allowUserInteraction) boolean getAllowUserInteraction () void setConnectTimeout (int timeout) int getConnectTimeout () void setReadTimeout (int timeout) int getReadTimeout () void setRequestProperty (String key,String value) Map<String,List<String>> getRequestProperties () void connect () Map<String,List<String>> getHeaderFields () String getHeaderFieldKey (int n) String getHeaderField (int n) int getContentLength () String getContentEncoding () long getDate () long getExpiration () long getLastModified () InputStream getInputStream () OutputStream getOutputStream () Object getContent ()
操作步骤 1 2 3 4 5 6 7 URLConnection urlConnection = url.openConnection();