Tag:android file download
public static void Downfile (final String URL) {new Thread () {public void run () {FILEOUTPUTSTR EAM Os=null; try{InputStream Input=null; URLConnection httpurlconnection= new URL (URL). OpenConnection (); int contentlength = Httpurlconnection.getcontentlength (); Input =httpurlconnection.getinputstream (); File File = new file ("c:\\users\\administrator\\desktop\\apknew.apk"); If the destination file already exists, delete it. Produces an effect that overwrites the old file if (file.exists ()) {file.delete (); } OS = new FileOutputStream (file); byte[] buffer = new byte[4*1024]; The length of the data read int len; while ((Len=input.read (buffer))! =-1) {os.write (Buffer,0,len); This cannot be written as Os.write (buffer)} os.flush (); Os.close (); Input.close ();//You must not forget to close the input stream//log.v ("cmd", "File download complete, path:" +File.getabsolutepath ()); Update (); System.out.println ("Download Complete"); } catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); }finally{}}}.start (); }
Recently do the project with the file download, so Baidu has more than n download code, and then copy, I am here to download the APK, and repeatedly are files can be downloaded, but the installation of the APK is a parsing package error. Two errors were found by patient examination
! 1. Input stream forgot to close
2, Os.write (Buffer,0,len) written os.write (buffer) Baidu came out of a lot of this is written, and Eclipse does not error what is the reason for expecting the great God guidance Ah!