Android development tool Class 26_getfile

Source: Internet
Author: User

Getting executables from the network

1  Public voidGetFile ()throwsexception{2 3     //get the requested path first4String URLPath = "Http://ftpcnc-js.pconline.com.cn/pub/download/201003/Fetioon_3.6.1900.exe";5URL url =NewURL (urlpath);6HttpURLConnection conn =(HttpURLConnection) url.openconnection ();7Conn.setrequestmethod ("GET");8Conn.setconnecttimeout (6*1000);9     if(Conn.getresponsecode () = = 200){TenInputStream InputStream =Conn.getinputstream (); One         byte[] data =Readinstream (inputstream); AFile File =NewFile ("Feixin.exe"); -FileOutputStream OutputStream =Newfileoutputstream (file); - outputstream.write (data); the outputstream.close (); -     } -}

Readinstream

1 //read the contents of a stream file2  Public  byte[] Readinstream (InputStream inputstream)throwsexception{3 4Bytearrayoutputstream Bytearrayoutputstream =5             NewBytearrayoutputstream ();6     byte[] buffer =New byte[1024];7     intLength =-1;8      while(length = inputstream.read (buffer))! =-1){9Bytearrayoutputstream.write (buffer, 0, length);Ten     } One bytearrayoutputstream.close (); A inputstream.close (); -     returnBytearrayoutputstream.tobytearray (); -}

Android development tool Class 26_getfile

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.