• Android基于HttpUrlConnection类的文件下载


     1   /**
     2      * get方法的文件下载
     3      * <p>
     4      * 特别说明 android中的progressBar是google唯一的做了处理的可以在子线程中更新UI的控件
     5      *
     6      * @param path
     7      */
     8     private void httpDown(final String path) {
     9         new Thread() {
    10             @Override
    11             public void run() {
    12                 URL url;
    13                 HttpURLConnection connection;
    14                 try {
    15                     //统一资源
    16                     url = new URL(path);
    17                     //打开链接
    18                     connection = (HttpURLConnection) url.openConnection();
    19                     //设置链接超时
    20                     connection.setConnectTimeout(4000);
    21                     //设置允许得到服务器的输入流,默认为true可以不用设置
    22                     connection.setDoInput(true);
    23                     //设置允许向服务器写入数据,一般get方法不会设置,大多用在post方法,默认为false
    24                     connection.setDoOutput(true);//此处只是为了方法说明
    25                     //设置请求方法
    26                     connection.setRequestMethod("GET");
    27                     //设置请求的字符编码
    28                     connection.setRequestProperty("Charset", "utf-8");
    29                     //设置connection打开链接资源
    30                     connection.connect();
    31 
    32                     //得到链接地址中的file路径
    33                     String urlFilePath = connection.getURL().getFile();
    34                     //得到url地址总文件名 file的separatorChar参数表示文件分离符
    35                     String fileName = urlFilePath.substring(urlFilePath.lastIndexOf(File.separatorChar) + 1);
    36                     //创建一个文件对象用于存储下载的文件 此次的getFilesDir()方法只有在继承至Context类的类中
    37                     // 可以直接调用其他类中必须通过Context对象才能调用,得到的是内部存储中此应用包名下的文件路径
    38                     //如果使用外部存储的话需要添加文件读写权限,5.0以上的系统需要动态获取权限 此处不在不做过多说明。
    39                     File file = new File(getFilesDir(), fileName);
    40                     //创建一个文件输出流
    41                     FileOutputStream outputStream = new FileOutputStream(file);
    42 
    43                     //得到链接的响应码 200为成功
    44                     int responseCode = connection.getResponseCode();
    45                     if (responseCode == HttpURLConnection.HTTP_OK) {
    46                         //得到服务器响应的输入流
    47                         InputStream inputStream = connection.getInputStream();
    48                         //获取请求的内容总长度
    49                         int contentLength = connection.getContentLength();
    50 
    51                         //设置progressBar的Max
    52                         mPb.setMax(contentLength);
    53 
    54 
    55                         //创建缓冲输入流对象,相对于inputStream效率要高一些
    56                         BufferedInputStream bfi = new BufferedInputStream(inputStream);
    57                         //此处的len表示每次循环读取的内容长度
    58                         int len;
    59                         //已经读取的总长度
    60                         int totle = 0;
    61                         //bytes是用于存储每次读取出来的内容
    62                         byte[] bytes = new byte[1024];
    63                         while ((len = bfi.read(bytes)) != -1) {
    64                             //每次读取完了都将len累加在totle里
    65                             totle += len;
    66                             //每次读取的都更新一次progressBar
    67                             mPb.setProgress(totle);
    68                             //通过文件输出流写入从服务器中读取的数据
    69                             outputStream.write(bytes, 0, len);
    70                         }
    71                         //关闭打开的流对象
    72                         outputStream.close();
    73                         inputStream.close();
    74                         bfi.close();
    75 
    76                         runOnUiThread(new Runnable() {
    77                             @Override
    78                             public void run() {
    79                                 Toast.makeText(MainActivity.this, "下载完成!", Toast.LENGTH_SHORT).show();
    80                             }
    81                         });
    82                     }
    83 
    84                 } catch (Exception e) {
    85                     e.printStackTrace();
    86                 }
    87             }
    88         }.start();
    89     }

    有不清楚的地方欢迎各位朋友们留言

  • 相关阅读:
    cache buffers chains latch
    freemarker自定义标签报错(七)
    freemarker自定义标签(三)-nested指令
    freemarker自定义标签(二)
    Buffer Cache 原理
    JavaScript去除日期中的“-”
    JavaScript替换HTML标签
    JavaScript获取地址栏中的参数
    JavaScript中的indexOf
    Java中的字符串拼接
  • 原文地址:https://www.cnblogs.com/hejiaoshou/p/7463318.html
Copyright © 2020-2023  润新知