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 ));
}