import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;

public class DownLoader {
public int progress = 0;
public int flag = 0;// 判断是第几次点击
public static int remainFileSize = 0;
static Context context;

public DownLoader(Context context) {
DownLoader.context = context;

}

private static DownLoader instance = null;

public static DownLoader getInstance(Context context) {// Context context
DownLoader.context = context;
if (instance == null) {
instance = new DownLoader(context);
}
return instance;
}

public HttpURLConnection conn = null;

public void startDownload(String urlString) {

File savePath = new File(Environment.getExternalStorageDirectory()
.toString() + "/lenovo");

String saveName = urlString.substring(urlString.lastIndexOf("/") + 1);
Log.e("name", saveName);
savePath.mkdirs();

File saveTo = new File(savePath, saveName);
if (!saveTo.exists())
try {
saveTo.createNewFile();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
InputStream is = null;
BufferedOutputStream fos = null;

int status = -1;

long downloadedFileSize = 0;

if (saveTo != null)
downloadedFileSize = saveTo.length();

try {
URL url = new URL(urlString);

conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(30 * 1000);
conn.setReadTimeout(30 * 1000);
conn.setRequestProperty("Range", "bytes=" + downloadedFileSize
+ "-");

status = conn.getResponseCode();
if (status == HttpURLConnection.HTTP_OK
|| status == HttpURLConnection.HTTP_PARTIAL) {

if (flag == 0) {
remainFileSize = conn.getContentLength();
}

is = conn.getInputStream();

fos = new BufferedOutputStream(new FileOutputStream(saveTo,
true));

int lenth = -1;
byte[] buffer = new byte[1024];
while ((lenth = is.read(buffer)) != -1) {
progress = (int) (downloadedFileSize * 100.0 / remainFileSize);
downloadedFileSize = downloadedFileSize + lenth;

if (downloadedFileSize <= remainFileSize)
fos.write(buffer, 0, lenth);

Intent intent = new Intent("update_progressbar");
intent.putExtra("progress", progress);
context.sendBroadcast(intent);

if (downloadedFileSize == remainFileSize) {
Intent intent2 = new Intent("update_button");
context.sendBroadcast(intent2);
}

}
} else {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (conn != null) {
conn.disconnect();
}

}
}

}

更多相关文章

  1. android GestureDetector 手势的判断
  2. android 流布局自动换行和居中
  3. android两次点击返回键退出和按钮退出对话框
  4. Android判断当前网络类型是否为5G
  5. android,如果某个文件存在就删除
  6. Android转战开发Kotlin
  7. Android(安卓)EditText 只能输入三行文本的实现方法
  8. android 给textview部分文字设置点击事件
  9. android 如何判断当前进程在主页面

随机推荐

  1. Android之进程与线程
  2. Android(安卓)NDK环境搭建
  3. android静默安装的实现(一)
  4. Android项目中集成React Native
  5. Android进程与线程基本知识
  6. Android的驱动程序(一)
  7. Android(安卓)技术专题系列之一 -- Android
  8. Android(安卓)Intent的几种用法全面总结
  9. Fedora 下配置 Android(安卓)开发环境
  10. Android使用Fragment打造万能页面切换框