zoukankan      html  css  js  c++  java
  • 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();
        }
    
    
  • 相关阅读:
    关于CQRS(老外经典好文)
    关于Autofac的使用陷阱
    文件写入文件分布式系统(asp.net C#)
    NET Framework 4.5.2
    asp.net开源
    关于委托:异常{ 无法将 匿名方法 转换为类型“System.Delegate”,因为它不是委托类型 }
    sql SELECT时的with(nolock)选项说明
    树形结构的数据库表Schema设计
    C#操作符??和?:
    Byte[]和BASE64之间的转换
  • 原文地址:https://www.cnblogs.com/gbhh/p/13768068.html
Copyright © 2011-2022 走看看