Java reads network files

Source: Internet
Author: User
1. http:

/**

* Get an object through HTTP
*
* @ Param strurl
* @ Param filename
* @ Return
* @ Throws ioexception
*/
Private
Boolean getremotefile (string strurl, string filename)
Throws ioexception {
URL url = new URL (strurl );
Httpurlconnection conn = (httpurlconnection) URL. openconnection ();
Datainputstream input = new datainputstream (conn. getinputstream ());

Dataoutputstream output = new dataoutputstream (New fileoutputstream (filename ));

Byte [] buffer =
New byte [1, 1024*8];
Int COUNT = 0;
While (COUNT = input. Read (buffer)> 0 ){

Output. Write (buffer, 0, count );
}
Output. Close ();
Input. Close ();
Return true;

When calling, use the data test below, and then get test.gif: String fileurl = "http://www.google.cn/intl/zh-CN/images/logo_cn.gif ";

String filename = "test.gif"; FTP-based access is supported. You only need to change it as follows: // httpurlconnection conn = (httpurlconnection) URL. openconnection ();

Urlconnection conn = url.openconnection(please refer to the test code below for successful execution. Please refer to cu_html.zip file: Public
Static void main (string [] ARGs)
Throws ioexception {
String fileurl = "ftp://ftp.cuhk.hk/pub/cu_html.zip ";

String filename = "cu_html.zip ";

Test1 test = new test1 ();

System. Out. println (test. getremotefile (fileurl, filename ));
}

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.