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

Source: Internet
Author: User
Tags date connect
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!





Please see this is the implementation of the principle of the source program:








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:");


//Read the user input URL


int Ount=system.in.read (BUF);





string Addr=new string


(Buf,0,count);





//The URL string entered by the user is passed into the URL class object


url url=new url (addr);


//creates the URLConnection object and returns the connection to the URLConnection object using the OpenConnection method of the URL


//The OpenConnection return value of the URL is actually a urlconnection


urlconnection C = url.openconnection ();


Establish a connection
with URLConnection Connect () method

C.connect ();


//Displays information about the connection, these are 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 ("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.