Download files under Android

Source: Internet
Author: User

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!

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.