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 com.lenovo.lsf.push.messagecenter.db.FileDownload;
import com.lenovo.lsf.push.messagecenter.db.FileDownloadDAO;
import com.lenovo.lsf.push.messagecenter.db.IFileDownloadDAO;

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

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

private IFileDownloadDAO fileDAO;

public DownLoader(Context context) {
DownLoader.context = context;
fileDAO = new FileDownloadDAO(context);

}

public HttpURLConnection conn = null;

public void startDownload(String urlString,String FBID,int theUpdateProgressBarNumber) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {


FileDownload fileDownload = new FileDownload();
fileDownload.setFBID(FBID);

if (null != conn)
conn.disconnect();

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

//String saveName = urlString.substring(urlString.lastIndexOf("/") + 1);
String saveName = FBID;



fileDownload.setFileName(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();
fileDownload.setFileSize(conn.getContentLength());
}

if (!fileDAO.queryAllFBID().contains(
fileDownload.getFBID()))
fileDAO.addFile(fileDownload);

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 / fileDAO
.selectFileSizeByFileName(saveName));
downloadedFileSize = downloadedFileSize + lenth;

if (downloadedFileSize <= fileDAO
.selectFileSizeByFileName(saveName))
fos.write(buffer, 0, lenth);

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

if (downloadedFileSize == fileDAO
.selectFileSizeByFileName(saveName)) {
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();
}

}
}
else
{
Toast.makeText(context, "SD卡不可用", Toast.LENGTH_LONG);

}
}

}

更多相关文章

  1. android之点击事件ImageView切换
  2. android的四种点击事件的设置
  3. android 之Dialog对话框(简易版)
  4. Android(安卓)点击按钮,文本文字改变
  5. android识别 单击和双击事件
  6. 我的Android计算器
  7. Android(安卓)service生命周期详解
  8. android camera2 api点击图片实现聚焦
  9. Android(安卓)监听Fragment界面的点击事件

随机推荐

  1. MySQL CHAR和VARCHAR该如何选择
  2. 带你学习MySQL执行计划
  3. MySQL完整性约束的定义与实例教程
  4. 解决Navicat for MySQL 连接 MySQL 报200
  5. MYSQL(电话号码,身份证)数据脱敏的实现
  6. MySql开发之自动同步表结构
  7. mysql升级到5.7时,wordpress导数据报错10
  8. 解决Navicat for Mysql连接报错1251的问
  9. Navicat for MySQL的使用教程详解
  10. MySQL Router实现MySQL的读写分离的方法