Java教程

网络编程相关知识

本文主要是介绍网络编程相关知识,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

表示ip类的inetAddress

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Demo1 {


    public static void main(String[] args) {
        try {
            //查询本机ip地址
            InetAddress inetAddress=InetAddress.getByName("127.0.0.1");     //没有构造器,不能new InetAddress()来创建对象,
                                                                            // 只能通过类名.静态方法来返回这样一个类继而创建对象
            System.out.println(inetAddress);
            inetAddress=InetAddress.getByName("localhost");
            System.out.println(inetAddress);
            inetAddress=InetAddress.getLocalHost();    //为啥这个方法获得的本机地址和上面不一样
            System.out.println(inetAddress);
            //查询网站ip地址
            inetAddress=InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress);
            //常用方法
            System.out.println(inetAddress.getAddress());
            System.out.println(inetAddress.getCanonicalHostName());
            System.out.println(inetAddress.getHostAddress());
            System.out.println(inetAddress.getHostName());


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

有关端口的类以及方法
import java.net.InetSocketAddress;

public class Demo2 {

     public static void main(String[] args) {
        InetSocketAddress inetSocketAddress=new InetSocketAddress("127.0.0.1",8080);
        System.out.println(inetSocketAddress);
        System.out.println(inetSocketAddress.getAddress());
        System.out.println(inetSocketAddress.getHostName());
        System.out.println(inetSocketAddress.getPort());
         inetSocketAddress=new InetSocketAddress("localhost",8080);
        System.out.println(inetSocketAddress);
    }
}

有关URL的相关方法

import java.net.MalformedURLException;
import java.net.URL;

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

下载网络上的图片,这里用到了io流

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


public class URLdownload {
    public static void main(String[] args) throws IOException {
        URL url=new URL("https://tse1-mm.cn.bing.net/th/id/R-C.250f95855aec49c974acb36b3ed32571?rik=BgcOI1PX1fb%2bww&riu=http%3a%2f%2fwww.desktx.com%2fd%2ffile%2fwallpaper%2fscenery%2f20170120%2f387b1a5181ebeddbec90fd5f19e606ce.jpg&ehk=Feb%2bz1leZKOVuTS20av3z7LKELRP0HH277cc6aSrAeI%3d&risl=&pid=ImgRaw");
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
                   InputStream inputStream=urlConnection.getInputStream();
        FileOutputStream fileOutputStream=new FileOutputStream("D:/xin.jpg");
        byte[] n=new byte[1024*1024];
        int m;
        if((m=inputStream.read(n))!=-1)
      fileOutputStream.write(n,0,m);
            fileOutputStream.flush();
            inputStream.close();
            fileOutputStream.close();
urlConnection.disconnect();
    }
}
这篇关于网络编程相关知识的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!