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 apk断点续传更新 断点续传ios
平时项目开发中,经常遇到下载视频、语音、图片等等,其中断点续传是最常见的,当然这也是根据产品需求而定的,如果文件很小,就用不到断点,嗖地一下就下载好了。断点续传可以用苹果原生的方法,也可以用AFNetworking。 本节先讲苹果原生的文件下载方法,这里需要了解NSURLSession:一、NSURLSession简介NSURLConnection在iOS9被宣布弃用,NSURLSession是
android apk断点续传更新 NSURLSessionTask 断点续传 NSURLSession task