Android中使用多线程机制,可以实现文件下载,如果用户出现意外而导致下载未完成的情况下,当用户下次继续下载该文件时,系统会从该用户上一次下载的大小继续下载,从而提高了用户的信任度,体搞了效率。

下面是一个例子:

package com.hbsi.csdn.multile;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;


public class test {


public static final String path = "http://192.168.64.138:8080/youdaocidian.exe";
public static int threadcount;


public static void main(String[] args) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");// 设置请求方式
conn.setConnectTimeout(5000); // 设置获取响应码的时间


int len = conn.getContentLength(); // 获取相应数据的长度
System.out.println("下载的文件的长度是:" + len);


// 在客户端创建一个大小跟服务区端文件一样的资源
File file = new File("youdao.exe");
RandomAccessFile randomfile = new RandomAccessFile(file, "rwd");
randomfile.setLength(len);


// 假设开启5个子线程
int blocksize = len / 5;
threadcount = 0;
for (int i = 1; i <= 5; i++) {
int startsize = (i - 1) * blocksize;
int endsize = (i) * blocksize - 1;
if (i == 5) {
endsize = len;
}
new Thread(new DownLoadTask(i, startsize, endsize)).start();
// 解决所有的子线程全部执行完毕后,在把所有的i.txt文件删除
}


}


}


class DownLoadTask implements Runnable {
private int id, startsize, endsize;


public DownLoadTask(int id, int startsize, int endsize) {
super();
this.id = id; // 线程的id
this.startsize = startsize;
this.endsize = endsize;
}


@Override
public void run() {


try {
// 每一个线程创建执行时都创建一个id.txt的文件,这个文件用来记载当前线程的下载进度。
File idfile = new File(id + ".txt");
if (idfile.exists()) {
FileInputStream fis = new FileInputStream(idfile);
ByteArrayOutputStream boas = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
boas.write(buffer, 0, len);
}
fis.close();
boas.close();
byte[] result = boas.toByteArray();
String numberstr = new String(result);
if (numberstr != null && (!"".equals(numberstr))) {
int startposition = Integer.parseInt(numberstr);
if (startposition > startsize) {
startsize = startposition; // 重新指定下载的开始位置
}
}
}


URL url = new URL(test.path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");// 设置请求方式
conn.setConnectTimeout(5000); // 设置获取响应码的时间
// 指定当前线程从服务器上的哪个位置下载
conn.setRequestProperty("Range", "bytes=" + startsize + "-"
+ endsize);
System.out.println("文件下载的开始位置:" + startsize + " 结束位置:"
+ endsize);
InputStream is = conn.getInputStream();
File file = new File("youdao.exe");
RandomAccessFile randomfile = new RandomAccessFile(file, "rwd");
randomfile.seek(startsize);
byte[] buffer = new byte[1024];
int len = 0;
int tatol = 0;
while ((len = is.read(buffer)) != -1) {
randomfile.write(buffer, 0, len);
tatol += len;
FileOutputStream idfos = new FileOutputStream(idfile);
idfos.write(("" + (startsize + tatol)).getBytes());
idfos.flush();
idfos.close();
}
randomfile.close();
is.close();
System.out.println("线程 " + id + " 下载完毕");


/*
* if(idfile.exists()){ idfile.delete(); }
*/
synchronized (test.class) {
test.threadcount++;
if (test.threadcount >= 5) {
for (int i = 1; i <= 5; i++) {
File deletefile = new File(i + ".txt");
deletefile.delete();
}
}
}


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}

更多相关文章

  1. Android——使用ContentProvider在应用间传递数据
  2. Android(安卓)Selector用法小记
  3. Android异常汇集----4. Android(安卓)requires compiler complia
  4. git的使用(上传项目到github)
  5. 第四章 Android开发三大基石—Activity、Service和Handler(2)
  6. android opengl 原理及开发(2)绘制基本图形
  7. android 知识点总结 广播接收器生命周期【爱扒拉】
  8. Android调用第三方so库
  9. android style用法

随机推荐

  1. INSTALL_FAILED_TEST_ONLY
  2. Android(安卓)EditText 密码隐藏或可见
  3. Android(安卓)studio 导出项目为jar
  4. 关于Android中TextView显示多个空格
  5. Android如何根据当前显示配置匹配资源 la
  6. Android--EditText控件属性汇总
  7. Android——SpannableString实现带圆角标
  8. Android(安卓)关于倒计时功能的实现
  9. Android各分辨率定义的图片规格
  10. Android(安卓)实用类