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 点击按钮,文本文字改变
- android 添加桌面窗口小部件(托至桌面需要点击按钮才能生成的)
- Android动态生成按钮样式
- Android为按钮添加相应事件的代码
- 给按钮设置点击变色
- Android添加圆角渐变色按钮
- Android——按钮的事件监听
- Button 按钮的几个属性