1 /**2 * TODO download files to local3 * @authorNadim4 * @date Sep One, 11:45:31 AM5 * @paramFILEURL Remote Address6 * @paramfilelocal Local Path7 * @throwsException8 */9 Public voidDownloadFile (String fileurl,string filelocal)throwsException {TenURL url =NewURL (FILEURL); OneHttpURLConnection Urlcon =(HttpURLConnection) url.openconnection (); AUrlcon.setconnecttimeout (6000); -Urlcon.setreadtimeout (6000); - intCode =Urlcon.getresponsecode (); the if(Code! =HTTPURLCONNECTION.HTTP_OK) { - Throw NewException ("file read failed"); - } - + //Read file stream -DataInputStream in =NewDataInputStream (Urlcon.getinputstream ()); +DataOutputStream out =NewDataOutputStream (NewFileOutputStream (filelocal)); A byte[] buffer =New byte[2048]; at intCount = 0; - while((count = in.read (buffer)) > 0) { -Out.write (buffer, 0, count); - } - out.close (); - in.close (); in}
JAVA implementation download file to local library