zoukankan      html  css  js  c++  java
  • Java 基础 (网络编程 UDP 例子, URL)

    UDP

    UDPTest.java

    package com.klvchen.java1;
    
    import org.junit.Test;
    
    import java.io.IOException;
    import java.net.*;
    
    public class UDPTest {
    
        @Test
        public void sender() throws IOException {
    
            DatagramSocket socket = new DatagramSocket();
    
            String str = "我是UDP方式发送的导弹";
            byte[] data = str.getBytes();
            InetAddress inet = InetAddress.getLocalHost();
            DatagramPacket packet = new DatagramPacket(data, 0, data.length,inet,9090);
    
            socket.send(packet);
    
            socket.close();
    
        }
    
        @Test
        public void receiver() throws IOException{
    
            DatagramSocket socket = new DatagramSocket(9090);
    
            byte[] buffer = new byte[100];
            DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
    
            socket.receive(packet);
    
            System.out.println(new String(packet.getData(), 0, packet.getLength()));
    
            socket.close();
        }
    }
    

    URL

    * URL(Uniform Resource Locator): 统一资源定位符,它表示 Internet 上某一资源的地址。
    * 它是一种具体的URI,即URL可以用来标识一个资源,而且还指明了如何locate这个资源。
    * 通过URL我们可以访问 Internet上的各种网络资源,比如最常见的 www,ftp站点。浏览器通过解析给定的URL可以在网络上查找相应的文件或其他资源。
    
    * URL的基本结构由5部分组成:
    <传输协议>://<主机名>:<端口号>/<文件名>#片段名?参数列表
    例如:
    http://192.168.1.100:8080/helloworld/index.jsp#a?username=shkstart&password=123
    
    

    URLTest.java

    package com.klvchen.java1;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class URLTest {
    
        public static void main(String[] args){
    
            URL url = null;
            try {
                url = new URL("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201810%2F05%2F20181005142528_yinka.jpg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638864631&t=26377d1d4652702f21909f93fee1af0e");
    
                // 获取URL的协议名称
                System.out.println(url.getProtocol());
                // 获取该URL的主机名
                System.out.println(url.getHost());
                // 获取该URL的端口号
                System.out.println(url.getPort());
                //获取该URL的文件路径
                System.out.println(url.getPath());
                //获取该URL的文件名
                System.out.println(url.getFile());
                // 获取该URL的查询名
                System.out.println(url.getQuery());
    
    
    
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
    
    
    
        }
    }
    
    

    URLTest1.java

    package com.klvchen.java1;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class URLTest1 {
    
        public static void main(String[] args){
    
            HttpURLConnection urlConnection = null;
            InputStream is = null;
            FileOutputStream fos = null;
            try {
                URL url = new URL("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201810%2F05%2F20181005142528_yinka.jpg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638864631&t=26377d1d4652702f21909f93fee1af0e");
    
                urlConnection = (HttpURLConnection) url.openConnection();
    
                urlConnection.connect();
    
                is = urlConnection.getInputStream();
                fos = new FileOutputStream("day10\\5.jpeg");
    
                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 (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                }
                if (urlConnection != null) {
                    urlConnection.disconnect();
    
                }
            }
    
        }
    }
    
    
  • 相关阅读:
    OpenCV之图像归一化(normalize)
    虚拟机网络模式说明
    大端模式和小端模式
    ASCII、Unicode、UTF-8、UTF-8(without BOM)、UTF-16、UTF-32傻傻分不清
    MFC窗口通过OpenCV显示图片
    基于FFmpeg的Dxva2硬解码及Direct3D显示(五)
    PyQt5播放实时视频流或本地视频文件
    PyQt5信号与槽关联的两种方式
    PyCharm离线安装PyQt5_tools(QtDesigner)
    DC: 8
  • 原文地址:https://www.cnblogs.com/klvchen/p/15520020.html
Copyright © 2011-2022 走看看