zoukankan      html  css  js  c++  java
  • Android 普通文件下载

      /**
       * 下载文件
       */
      private void writeFile(Response response) {
            InputStream is = null;
            FileOutputStream fos = null;
            is = response.body().byteStream();
            String path = Environment.getExternalStorageDirectory().getAbsolutePath();
            File file = new File(path, fileName);
            try {
                fos = new FileOutputStream(file);
                byte[] bytes = new byte[2048];
                int len = 0;
                long totalSize = response.body().contentLength();
                long sum = 0;
                while ((len = is.read(bytes)) != -1) {
                    fos.write(bytes);
                    sum += len;
                    int press = (int) ((sum * 1.0f / totalSize) * 100);
                    Message msg = handler.obtainMessage(1);
                    msg.arg1 = press;
                    handler.sendMessage(msg);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                    if (fos != null) {
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 设置进度
         */
        private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    int mpress = msg.arg1;
                    Log.i("----------进度:", String.valueOf(mpress));
                    progress.setProgress(mpress);
                }
            }
        };
  • 相关阅读:
    函数的声明
    数组遍历的方法
    运算符
    变量命名规则
    js条件判断
    vuex使用
    高盛伦敦分部面试
    野村证券伦敦分部面试
    Linux Performance Monitoring Commands
    Linux server上too many open files问题
  • 原文地址:https://www.cnblogs.com/xiaoyao095/p/6970819.html
Copyright © 2011-2022 走看看