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