Android 开发工具类 26_getFile

从网络获取可执行文件

 1 public void getFile() throws Exception{
 2 
 3     // 首先得到请求的路径
 4     String  urlpath = "http://ftpcnc-js.pconline.com.cn/pub/download/201003/Fetioon_3.6.1900.exe";
 5     URL  url = new URL(urlpath);
 6     HttpURLConnection  conn = (HttpURLConnection)url.openConnection();
 7     conn.setRequestMethod("GET");
 8     conn.setConnectTimeout(6*1000);
 9     if(conn.getResponseCode() == 200){
10         InputStream  inputStream = conn.getInputStream();
11         byte[]  data = readInstream(inputStream);
12         File  file = new File("feixin.exe");
13         FileOutputStream  outputStream = new FileOutputStream(file);
14         outputStream.write(data);
15         outputStream.close();
16     }
17 }

readInstream

 1 // 读取流文件的内容
 2 public  byte[] readInstream(InputStream inputStream) throws Exception{
 3 
 4     ByteArrayOutputStream  byteArrayOutputStream = 
 5             new  ByteArrayOutputStream();
 6     byte[] buffer = new byte[1024];
 7     int length = -1;
 8     while((length = inputStream.read(buffer)) != -1){
 9         byteArrayOutputStream.write(buffer, 0, length);
10     }
11     byteArrayOutputStream.close();
12     inputStream.close();
13     return  byteArrayOutputStream.toByteArray();
14 }
原文地址:https://www.cnblogs.com/renzimu/p/4540181.html