前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java中讲讲URL的用法,举例?

java中讲讲URL的用法,举例?

作者头像
马克java社区
修改2021-05-11 10:05:11
4200
修改2021-05-11 10:05:11
举报
文章被收录于专栏:java大数据

4.URL类的实例

马克-to-win:URL(Uniform Resource Locator-----一致资源查找器)它用来指向Internet上的资源文件,比如 http://java.sun.com:8080/docs/introdiction.htm net包中的URL类提供API来访问Internet上的信息。

比如以上的URL中:

1)协议:http

2)IP 地址或主机名:java.sun.com

3)端口号:8080

4)实际文件路径:docs/introdiction.htm

例:2.4.1

/*no need to have network through to run the program.*/

import java.net.*;

import java.io.*;

public class TestMark_to_win {

    public static void main(String[] args) throws Exception {

        /* Class URL represents a Uniform Resource Locator, a pointer to a

         * "resource" on the World Wide Web. A resource can be something as

         * simple as a file or a directory, public URL(String spec)throws

         * MalformedURLException: Creates a URL object from the String

         * representation.

         */

        URL aURL = new URL("http://java.sun.com:8080/docs/books/"

                + "tutorial/index.html");

        System.out.println("protocol = " + aURL.getProtocol());

        System.out.println("host = " + aURL.getHost());

        System.out.println("filename = " + aURL.getFile());

        System.out.println("default port = " + aURL.getDefaultPort());

        System.out.println("port = " + aURL.getPort());

    }

}

 

例:2.4.2

/* This program needs an open connection to run.

*/

import java.net.*;

import java.io.*;

import java.util.*;

public class TestMark_to_win {

    public static void main(String[] args) throws Exception {

        URL yahoo = new URL("https://www.oracle.com/index.html");

        /* public URLConnection openConnection() throws IOException: Returns a

         * URLConnection object that represents a connection to the remote

         * object referred to by the URL.

         */

        URLConnection yahooConnection = yahoo.openConnection();

        /* public long getLastModified() Returns the value of the last-modified

         * header field. The result is the number of milliseconds since January

         * 1, 1970 GMT.

         */

        System.out.println("content LastModified"

                + new Date(yahooConnection.getLastModified()));

        /*public InputStream getInputStream()throws IOException: Returns an

         * input stream that reads from this open connection.

         */

        // DataInputStream in = new

        // DataInputStream(yahooConnection.getInputStream());

        BufferedReader in = new BufferedReader(new InputStreamReader(

                yahooConnection.getInputStream()));

        String inputLine;

        for (int i = 0; i < 25; i++) {

            inputLine = in.readLine();

            System.out.println(inputLine);

        }

        in.close();

    }

}

 

更多请见:https://blog.csdn.net/qq_44639795/article/details/102319021

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档