HttpURLConnection下载文件带进度Code
来源:互联网 发布:徐达 知乎 编辑:程序博客网 时间:2024/06/04 20:00
package com.louisgeek.checkappupdatebyfirim;import android.os.Environment;import android.util.Log;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.ProtocolException;import java.net.URL;/** * Created by louisgeek on 2016/8/19. */public class DownloadFileTool { // 获得存储卡的路径 private static String sd_path = Environment.getExternalStorageDirectory() + "/"; private static String filePath = sd_path + "MyFileDir/Test/"; private static String saveFileAllName = filePath + "QQ.exe"; public static void doDownloadThread() { new Thread(new DownloadFileThread()).start(); } private static class DownloadFileThread implements Runnable { @Override public void run() { FileOutputStream fileOutputStream=null; InputStream inputStream =null; // try { URL url = new URL("http://dldir1.qq.com/qqfile/qq/QQ8.6/18781/QQ8.6.exe"); //获取连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Charset", "UTF-8"); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); //打开连接 connection.connect(); //获取内容长度 int contentLength = connection.getContentLength(); File file = new File(filePath); // 判断文件目录是否存在 if (!file.exists()) { file.mkdir(); } //file.mkdirs(); //输入流 inputStream = connection.getInputStream(); File myFile = new File(saveFileAllName); //输出流 fileOutputStream = new FileOutputStream(myFile); byte[] bytes = new byte[1024]; // int index=0; long totalReaded = 0; int temp_Len; while ((temp_Len = inputStream.read(bytes)) != -1) { // bytes[index]= (byte) temp_Len; // index++; totalReaded += temp_Len; Log.i("XXXX", "run: totalReaded:" + totalReaded); long progress = totalReaded * 100 / contentLength; Log.i("XXXX", "run: progress:" + progress); fileOutputStream.write(bytes, 0, temp_Len); } /* byte[] bytes = new byte[1024]; while (inputStream.read(bytes)!=-1){ fileOutputStream.write(bytes); }*/ } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { if (fileOutputStream!=null){ fileOutputStream.close();} if (inputStream!=null){ inputStream.close();} } catch (IOException e) { e.printStackTrace(); } } } }}
0 0
- HttpURLConnection下载文件带进度Code
- 使用HttpURLConnection上传文件(带提示进度对话框)
- Java带进度多线程下载文件
- Retrofit+Rxjava 下载文件(带进度显示)
- 基于NoHttp+RxBus实现文件下载(带下载进度)
- 下载【带进度】
- 使用Retrofit+RxJava实现带进度下载文件
- Android文件下载 HttpURLConnection
- 使用HttpURLConnection下载文件
- 使用HttpURLConnection下载文件
- HttpUrlConnection实现文件下载
- 使用HttpURLConnection下载文件
- HttpURLConnection实现文件下载
- Retrofit下载文件进度
- 基于Retrofit+Okio+RxBus实现文件下载(带下载进度)
- 基于Retrofit+Okio+RxBus实现文件下载(带下载进度)
- 基于Retrofit+Okio+RxBus实现文件下载(带下载进度)
- 基于Retrofit+Okio+RxBus实现文件下载(带下载进度)
- storm如何部署拓扑
- 欢迎使用CSDN-markdown编辑器
- iOS - IOS8 设置TableView Separatorinset 分割线从边框顶端开始
- ubuntu14.04 怎么打开rar的文件
- angularJS下载站点-编译后的版本下载
- HttpURLConnection下载文件带进度Code
- UITableView——dataSource
- handler, Looper, Message三者的关系,作者写的很好很详细。
- 【opencv练习28 - 直方图计算】
- UITableView——delegate
- 数组名为常指针,与数组第一个元素地址相同
- 纯CSS实现气泡框
- 【华为OJ】由寻找等差数列(初级)到动态规划
- quartz1.8+jdk1.5入门学习