JavaSE_网络编程_URL类

来源:互联网 发布:mac怎样建立文件夹 编辑:程序博客网 时间:2024/06/13 23:47

URL

URI:表示一个统一资源标识符 (URI) 引用。 URL:类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,例如对数据库或搜索引擎的查询。

四部分组成:协议、存放资源的主机域名、端口、资源文件名(/)

创建

绝对路径构建
根据 String 表示形式创建 URL 对象。

URL(String spec)   

相对路径构建
通过在指定的上下文中对给定的 spec 进行解析创建 URL。

URL(URL context, String spec) 

常用方法

getProtocol()   //获取此 URL 的协议名称。getHost()   //获取此 URL 的主机名(如果适用)。getPort()   //获取此 URL 的端口号。getFile()   //获取此 URL 的文件名。getPath()   //获取此 URL 的路径部分。getRef()    //获取此 URL 的锚点(也称为“引用”)。getQuery()  //获取此 URL 的查询部分。

URLDemo01.java

package URL;import java.net.MalformedURLException;import java.net.URL;public class URLDemo01 {    public static void main(String[] args) throws MalformedURLException {        //绝对路径构建        URL url = new URL("http://www.baidu.com:80/index.html#aa?uname=yangzheng");        System.out.println("协议:"+url.getProtocol());        System.out.println("域名:"+url.getHost());//获取此 URL 的主机名(如果适用)。        System.out.println("端口:"+url.getPort());//获取此 URL 的端口号。        System.out.println("资源:"+url.getFile());//获取此 URL 的文件名。        System.out.println("相对路径:"+url.getPath());//获取此 URL 的路径部分。        System.out.println("锚点:"+url.getRef());//获取此 URL 的锚点(也称为“引用”)。        System.out.println("参数:"+url.getQuery());//获取此 URL 的查询部分。        url = new URL("http://www.baidu.com:80/a/");        //相对路径构建        url = new URL(url, "b");        System.out.println(url.toString());    }}

输出:
协议:http
域名:www.baidu.com
端口:80
资源:/index.html
相对路径:/index.html
锚点:aa?uname=yangzheng
参数:null
http://www.baidu.com:80/a/b

URLDemo02.java

package URL;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.net.URL;public class URLDemo02 {    public static void main(String[] args) throws UnsupportedEncodingException, IOException {        URL url = new URL("http://www.baidu.com");        BufferedReader br =                 new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));        BufferedWriter bw =                new BufferedWriter(new OutputStreamWriter(new FileOutputStream("baidu.html"), "utf-8"));        String msg = null;        while((msg = br.readLine()) != null) {            System.out.println(msg);            bw.append(msg);                     bw.newLine();        }        bw.flush();        bw.close();        br.close();    }}

输出:

<!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&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ 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;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2016&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

这时在项目的目录里已经产生了baidu.html
点击项目,按F5刷新->出现baidu.html文件
这里写图片描述
点开文件:
这里写图片描述
哈哈,图片显示不出来。。尴尬了。。。

0 0