The Java urlconnection class performs an HTTP connection to obtain the file content.

Source: Internet
Author: User

The urlconnection class of Java provides the rul connection function. I can test the HTTP connection,

You may also be able to connect to FTP and Other types. Further test is required.

After obtaining the connection, you can obtain inputstream from The RUL connection, so that you can use the read method of inputstream to read the file.

Example:

Public static Boolean httpdownload (string httpurl, string SaveFile ){

// Download the Network File
Int bytesum = 0;
Int byteread = 0;
 
URL url = NULL;
Try {
Url = new URL (httpurl );
} Catch (malformedurlexception E1 ){
// Todo auto-generated Catch Block
E1.printstacktrace ();
Return false;
}
 
Try {
Urlconnection conn = URL. openconnection ();
Inputstream instream = conn. getinputstream ();
Fileoutputstream FS = new fileoutputstream (SaveFile );
 
Byte [] buffer = new byte [1204];
While (byteread = instream. Read (buffer ))! =-1 ){
Bytesum + = byteread;
// System. Out. println (bytesum );
FS. Write (buffer, 0, byteread );
}
Return true;
} Catch (filenotfoundexception e ){
E. printstacktrace ();
Return false;
} Catch (ioexception e ){
E. printstacktrace ();
Return false;
}

}

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.