• URL


    概述

    URL(Uniform Resource Locator)统一资源定位器

    它是WWW的统一资源定位标志,就是指网络地址。

    使用方法

    下载网络URL资源

    • 创建URL对象,放入链接
    • 打开连接 openConnection
    • 获取输入流,读取到内存
    • 获取文件输出流,并设置存储路径
    • 建立缓冲区
    • 写入到本地
    • 关闭流和连接

    案例

    基本URL信息获取

    public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=gbhh&password=123");
        System.out.println(url.getProtocol());
        System.out.println(url.getHost());
        System.out.println(url.getPort());
        System.out.println(url.getFile());
        System.out.println(url.getPath());
        System.out.println(url.getQuery());
    
    }
    

    下载网络资源

    /**
     * 下载网络资源,通过URL
     */
    public class DownloadUrlData {
        public static void main(String[] args) throws IOException {
            //下载tomcat本地文件
    //        URL url = new URL("http://localhost:8080/gbhh/test.txt");
    //        下载浏览器上的网易云歌曲图片链接
            URL url = new URL("https://p1.music.126.net/JtevaRk1N7ecpmwZCIvwzQ==/109951165293262893.jpg?param=130y130");
            URLConnection urlConnection = url.openConnection();
            InputStream is = urlConnection.getInputStream();
    //        FileOutputStream fos = new FileOutputStream("socket/downloadUrl.txt");
            FileOutputStream fos = new FileOutputStream("socket/wyy.jpg");
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
    
            fos.close();
            is.close();
        }
    
    
  • 相关阅读:
    UVALive-3989 Ladies' Choice (稳定婚姻问题)
    UVA-11383 Golden Tiger Claw (KM算法)
    UVA-10816 Travel in Desert (最小瓶颈最短路)
    UVA-10369 Arctic Network (最小生成树)
    内核态信号量(todo)
    openBMC(todo)
    uboot on qemu
    install ubuntu iso on windows
    在qemu上运行linux
    todo:register_sysctl_table
  • 原文地址:https://www.cnblogs.com/gbhh/p/13768068.html
Copyright © 2020-2023  润新知