File Download (Java) Android

Source: Internet
Author: User
//// // The file download function is as follows/ //// // Private void downfile (string urlstring) {/** connect to the server */Double filelength = 0; double downedfilelength = 0; inputstream = NULL; urlconnection connection = NULL; outputstream = NULL; try {URL url = new URL (urlstring); connection = URL. openconnection (); If (connection. getreadtimeout () = 5) {log. I ("----------> "," The current network has a problem ");} inputstream = connection. getinputstream ();} catch (malformedurlexception E1) {// todo auto-generated catch blocke1.printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}/** specifies the path of the file to be saved on the SD card of the mobile phone. If not, create */string savepath = environment. getexternalstoragedirectory () + "/lzmdownload"; file file1 = new file (savepath); If (! File1.exists () {file1.mkdir ();} string savepathstring = environment. getexternalstoragedirectory () + "/lzmdownload/" + "test.png"; file = new file (savepathstring); try {L. L ("=================================== newfile:" + file. createnewfile (); file. createnewfile (); L. L ("=================================== newfile:" + file. createnewfile ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}/** write a file to the SD card and use the handle transmission thread */message = new message (); try {outputstream = new fileoutputstream (File ); byte [] buffer = new byte [1024*4]; filelength = connection. getcontentlength (); L. L ("========================== filesize:" + filelength); message. what = 0; handler. sendmessage (Message); While (downedfilelength <filelength) {outputstream. write (buffer); downedfilelength + = inputstream. read (buffer); I = (INT) (double) downedfilelength/filelength) * 100); message message1 = new message (); message1.what = 1; // downloading... the filehandler. sendmessage (message1);} message message2 = new message (); // finish the downloadmessage2.what = 2; handler. sendmessage (message2);} catch (filenotfoundexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}} /// // The above is a download class ////////////// ///////////////////////////////

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.