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