Read the files on the Web using the URL class package provided by Java

Source: Internet
Author: User

Java provided by the URL class package, we can like IE browser from the designated URL to download the Web page, and, the download is absolutely true HTML, using this principle we can make their own kernel of the browser it!

See this is the source of the implementation principle:

Import Java.net.URL;
Import java.net.URLConnection;
Import java.io.IOException;

Import Java.util.Date;

public class Urlc {void display () {byte buf[]=new byte[100];
try {System.out.print ("Please enter the URL address of the file:");

Reads the URL int ount=system.in.read (BUF) entered by the user;

String Addr=new string (Buf,0,count);
Passes the URL string entered by the user to the URL class object URL url=new url (addr);
Creates a URLConnection object that returns the connection to the URLConnection object with the OpenConnection method of the URL the OpenConnection return value of the URL is a urlconnection
URLConnection C = url.openconnection ();
The connection C.connect () is established by the URLConnection Connect () method;
Displays information about the connection, which is the URLConnection method System.out.println ("Content type:" +c.getcontenttype ());
SYSTEM.OUT.PRINTLN ("Content code:" +c.getcontentencoding ());
System.out.println ("Content Length:" +c.getcontentlength ());
System.out.println ("Date Created:" +new date (C.getdate ()));
System.out.println ("Last Modified:" +new date (c.getlastmodified ()));

System.out.println ("End Date:" +new date (C.getexpiration ()));

catch (IOException e) {System.out.println (e);} public static void Main (string[] args) {URLC aPp=new URLC ();
App.display (); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.