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 }
  • 相关阅读:
    C#面向对象编程基础-喜课堂笔记
    [爬虫]通过url获取连接地址中的数据
    第10季asp.net基础
    初学MVC
    学习MVC遇到的问题
    飞行棋小项目
    JAVAscript学习笔记
    iOS 清除xcode缓存和生成文件
    Access用OleDbParameter更新/插入数据
    SQLite动态库下载
  • 原文地址:https://www.cnblogs.com/renzimu/p/4540181.html
Copyright © 2011-2022 走看看