首页 > 编程语言 >网络编程

网络编程

时间:2022-11-01 23:56:06浏览次数:40  
标签:java URL 编程 bytes 网络 io import net

网络编程

三次握手和四次挥手

三次握手:
   A; 你瞅啥?
   B:  瞅你咋地?
   A:  干一架!
  
四次挥手:
    A:我要走了!
    B:你真的要走了吗?
    B:你真的真的要走了吗?
    A:我真的要走了!
    

根据URL 下载资源

package com.beijing.xiaowen.socket;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class UrlDown {
    public static void main(String[] args) throws IOException {
        //下载地址
        URL url = new URL("https://m801.music.126.net/20221102000733/eb291a2b2a7660d5cce1df71a4e32fba/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/21490161300/7d5d/ed47/a02a/37122022717520e5acfce1827ac12212.m4a");

        //连接到这个资源
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        InputStream inputStream = connection.getInputStream();

        FileOutputStream fileOutputStream = new FileOutputStream("abc.m4a");

        byte[] bytes = new byte[1024];
        int len;
        while ((len=inputStream.read(bytes))!=-1){
            fileOutputStream.write(bytes,0,len);
        }
        fileOutputStream.close();
        inputStream.close();
        connection.disconnect();
    }
}

标签:java,URL,编程,bytes,网络,io,import,net
From: https://www.cnblogs.com/always0708/p/16849631.html

相关文章