zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    2017年总结
    计算机基础之进制转换详解
    随机总数字里面选取随机数字进行随机排序案例(JAVA实现)
    XML学习入门
    位运算从入门到入狱第一弹----原创!
    MYSQL数据库引擎区别详解
    时间戳
    数据库三大范式整理
    数组
    JS全选与不选、反选
  • 原文地址:https://www.cnblogs.com/renzimu/p/4540181.html
Copyright © 2011-2022 走看看