Read Online files through the URL package provided by Java

Source: Internet
Author: User
Through the URL package provided by Java, we can download the webpage from the specified website like the IE browser, and the download is definitely the real HTML, with this principle, we can make our own kernel browser!

Please refer to the following source code for implementation principles:

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 ("Enter the URL of the file :");
// Read the URL entered by the user
Int ount = system. In. Read (BUF );

String ADDR = New String
(BUF, 0, count );

// Pass the URL string entered by the user into the URL Class Object
URL url = new URL (ADDR );
// Create a urlconnection object. Use the openconnection method of the URL to return the connection to the object of urlconnection.
// The actual URL's openconnection return value is a urlconnection
Urlconnection c = URL. openconnection ();
// Use the connect () method of urlconnection to establish a connection
C. Connect ();
// Display information about the connection. These are the urlconnection methods.
System. Out. println ("content type:" + C. getcontenttype ());
System. Out. println ("Content Encoding:" + C. getcontentencoding ());
System. Out. println ("Content Length:" + C. getcontentlength ());
System. Out. println ("creation date:" + new date (C. getdate ()));
System. Out. println ("Last modified Date:" + 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 ();
}
}

Related Article

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.