栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

Java进阶(四十八)网络编程4:URL类

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java进阶(四十八)网络编程4:URL类

一、URL类的理解与实例化 1.1概要
  • URL:统一资源定位符,表示Internet上某一资源的地址。
  • 它是一种具体的URI,即URL可以用来标识一个资源,而且还指明了如何locate这个资源。
  • 通过URL我们可以访问Internet上的各种网络资源,让比如最常见的www,ftp站点。浏览器通过解析给定的URL可以在网络上查找相应的文件或其他资源。
  • URL的基本结构由5部分组成:
    • <传输协议>://<主机名>:<端口号>/<文件名>#片段名?参数列表
    • #片段名:即错点,例如看小说,直接定位到章节
    • 参数列表格式:参数名=参数值&参数名=参数值....

1.2构造器
  • 为了表示URL,java.net中实现了类URL。可以通过以下构造器初始化URL对象:
    • URL(String spec):通过一个表示URL地址的字符串来构造对象。
      • 如 URL url = new URL("www.baidu.com");
    • URL(URL context, String spec):通过基URL和相对URL构造对象。
      • 如 URL url = new URL(url,"download.html");
    • URL(String protocol, String host, String file)
      • 如 URL url = new URL("http","www.baidu.com","download.html");
    • URL(String protocol, String host, int port, String file)
      • 如 URL url = new URL("http","www.baidu.com","80","download.html");

  • URL类的构造器都声明抛出非运行时一次,必须要对这一异常进行处理,通常是用try-catch语句进行捕获。

1.3常用方法

一个URL对象生成后,其属性不能被改变,可以通过给定的方法获取属性:

  • String getProtocol()    获取URL的协议名
  • String getHost()    获取URL的主机名
  • int getPort()    获取URL的端口号
  • String getPath()    获取URL的文件路径
  • String getFile()    获取URL的文件名
  • String getQuery()    获取URL的查询名
import java.net.MalformedURLException;
import java.net.URL;


public class URLTest {

    public static void main(String[] args) {

        try {
            URL url = new URL("http://127.0.0.1:8080/examples/servlets/cat.jpg");
//            String getProtocol()	获取URL的协议名
            System.out.println(url.getProtocol());

//            String getHost()	获取URL的主机名
            System.out.println(url.getHost());

//            int getPort()	获取URL的端口号
            System.out.println(url.getPort());

//            String getPath()	获取URL的文件路径
            System.out.println(url.getPath());

//            String getFile()	获取URL的文件名
            System.out.println(url.getFile());

//            String getQuery()	获取URL的查询名
            System.out.println(url.getQuery());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

    }

}
二、URL网络编程实现Tomcat服务端数据下载
  • 实例化URL对象。
  • 获得Connection对象,并连接。
  • 获取输入流,下载数据。
  • 关闭流资源。
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class URLTest2 {

    public static void main(String[] args) {

        InputStream is = null;
        FileOutputStream fos = null;
        try {
            //获得URL对象
            URL url = new URL("http://localhost:8090/examples/cat.jpg");

            //获得对应的Connection对象,并连接
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();

            //获取输入流,并下载数据
            is = urlConnection.getInputStream();
            fos = new FileOutputStream("cat.jpg");
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }

            System.out.println("下载完成!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1022906.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号