Android(安卓)断点续传
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);
}
}
}
更多相关文章
- android之点击事件ImageView切换
- android的四种点击事件的设置
- android 之Dialog对话框(简易版)
- Android(安卓)点击按钮,文本文字改变
- android识别 单击和双击事件
- 我的Android计算器
- Android(安卓)service生命周期详解
- android camera2 api点击图片实现聚焦
- Android(安卓)监听Fragment界面的点击事件