您的位置:首页 > 其它

多线程并发断点续传下载器

2015-06-03 16:50 232 查看

利用文件存储上次暂停时的下载量

大家可以改用sqlite3数据库来存储暂停时的数据量来完成断点下载,我就把我自己写的代码附上,大家共同学习,代码里面有大量的注释,如有逻辑不够清晰的地方,还请指点~~~~~~
下面是简图:
下面是主程序:

public class MainActivity extends Activity {
	public static final int UPDATE = 5;
	public static final int DOWM_FIN = 4;
	private EditText et_path;
	private ProgressBar pb;
	private Button btn;
	private TextView tv_process;
	public static int runningThread = 5;
	public static int threadcount = 5;
	public static final int DOWN_LOAD_ERROR = 1;
	public static final int SERVICE_ERROR = 2;

	public int currentProcess = 0;// 当前进度条的进度
	private Handler hander = new Handler() {
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case DOWN_LOAD_ERROR:
				Toast.makeText(getApplicationContext(), "下载失败", 0).show();
				break;
			case SERVICE_ERROR:
				Toast.makeText(getApplicationContext(), "服务失败", 0).show();
				break;
			case DOWM_FIN:
				Toast.makeText(getApplicationContext(), "下载完毕", 0).show();
				break;
			case UPDATE:
				//pb.setProgress(currentProcess);
				tv_process.setText("当前进度:" + pb.getProgress() * 100
						/ pb.getMax());
				break;

			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et_path = (EditText) findViewById(R.id.editText1);
		pb = (ProgressBar) findViewById(R.id.progressBar1);
		tv_process = (TextView) findViewById(R.id.textView1);
		btn = (Button) findViewById(R.id.button1);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						super.run();
						download();
					}
				}.start();
			}
		});
	}

	public void download() {
		final String path = et_path.getText().toString().trim();
		if (TextUtils.isEmpty(path)) {
			Toast.makeText(this, "下载路径错误", 0).show();
			return;
		}

		new Thread() {
			@Override
			public void run() {

				try {
					// 链接服务器,获取文件和长度;在本地创建一个大小跟服务器文件大小一样的额临时文件
					// String path =
					// "http://192.168.191.1:9011/wireshark-win32-1.2.4.exe";
					URL url = new URL(path);
					HttpURLConnection conn = (HttpURLConnection) url
							.openConnection();
					conn.setRequestMethod("GET");
					conn.setConnectTimeout(5000);
					int code = conn.getResponseCode();
					if (code == 200) {
						// 服务器返回的长度就是文件的长度
						int length = conn.getContentLength();
						System.out.println("文件长度" + length);
						pb.setMax(length);
						// 创建临时文件
						RandomAccessFile raf = new RandomAccessFile(
								"/storage/sdcard/stup.exe", "rwd");
						raf.setLength(length);
						raf.close();
						// 假设3个线程去下载资源
						// 平均每个线程下载的大小
						int blocksize = length / threadcount;
						for (int threadid = 1; threadid <= threadcount; threadid++) {
							int startIndex = (threadid - 1) * blocksize;
							int endIndex = threadid * blocksize - 1;
							if (threadid == threadcount) {
								endIndex = length;
							}
							System.out.println("线程" + threadid + "下载:"
									+ startIndex + "----->" + endIndex);
							new DownloadThread(path, threadid, startIndex,
									endIndex).start();
						}

					} else {
						System.out.println("服务器错误  ");
						Message msg = new Message();

						msg.what = SERVICE_ERROR;
						hander.sendMessage(msg);
					}

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Message msg = new Message();

					msg.what = DOWN_LOAD_ERROR;
					hander.sendMessage(msg);

				}
			}
		}.start();

	}

	public class DownloadThread extends Thread {

		private int threadid;
		private int startIndex;
		private int endIndex;
		private String path;

		public DownloadThread(String path, int threadid, int startIndex,
				int endIndex) {
			this.threadid = threadid;
			this.endIndex = endIndex;
			this.startIndex = startIndex;
			this.path = path;

		}

		@Override
		public void run() {
			// TODO Auto-generated method stub

			try {
				// 把下面的代码转换为数据库存储
				// ---------------sqlite3------------------------------
				// 检查是否纯在记录下载长度的文件,如果存在读取这个文件的数据
				File tempFile = new File("/storage/sdcard/" + threadid + ".txt");

				if (tempFile.exists() && tempFile.length() > 0) {
					FileInputStream fis = new FileInputStream(tempFile);
					byte[] temp = new byte[1024];
					int leng = fis.read(temp);

					String download = new String(temp, 0, leng);
					int downloadInt = Integer.parseInt(download);
					System.out.println("从已存文件读取的数据:" + threadid+":"+downloadInt);
					int alreadyDownload = downloadInt - startIndex;
					currentProcess += alreadyDownload;// 计算上次断点已经下载的 长度
					startIndex = downloadInt;// 修改下载真是的开始位置
					System.out.println("线程" + threadid+"下载的真实位置:" + startIndex);
					fis.close();
				}
				// ---------------------------------------------------
				// ---------------------------------------------------
				URL url = new URL(path);
				HttpURLConnection conn = (HttpURLConnection) url
						.openConnection();
				conn.setRequestMethod("GET");
				conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
						+ endIndex);
				System.out.println("线程真实下载:" + threadid + "下载:" + startIndex
						+ "----->" + endIndex);
				conn.setConnectTimeout(5000);
				int code = conn.getResponseCode();// 从服务器返回全部资源200,部分206;300资源不存在或重定向;400资源没有找到;500服务为武器内部出现错误
				// System.out.println(code);
				if (code == 206) {
					InputStream is = conn.getInputStream();
					RandomAccessFile raf = new RandomAccessFile(
							"/storage/sdcard/stup.exe", "rwd");
					raf.seek(startIndex);
					int len = 0;
					byte[] buffer = new byte[1024];
					int total = 0;// 已经下载de数据的长度

					while ((len = is.read(buffer)) != -1) {
						// 记录当前线程下咋的数据长度
						RandomAccessFile file = new RandomAccessFile(
								"/storage/sdcard/" + threadid + ".txt", "rwd");

						raf.write(buffer, 0, len);
						total += len;// 已经下载的数据长度
						//System.out.println(threadid + "已下载数据长度:"+ total);
						file.write((startIndex + total + "").getBytes());// 记录下载的位置,并不是进度
						file.close();
						// 更新进度条
						synchronized (MainActivity.this) {
							currentProcess += len;// 获取所有线程的总进度
						    pb.setProgress(currentProcess);// 更改界面进度//特殊
							Message msg = Message.obtain();
							msg.what = UPDATE;

							hander.sendMessage(msg);
						}

					}

					is.close();
					raf.close();
					System.out.println("线程" + threadid + "下载完毕");
				} else {
					System.out.println("线程" + threadid + "下载失败");
				}

			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				thread_finish();
			}

		}

		private synchronized void thread_finish() {
			runningThread--;
			if (runningThread == 0) {
				for (int i = 1; i <= 5; i++) {
					// 判断几个线程全部都执行完
					File delete = new File("/storage/sdcard/" + i + ".txt");
					delete.delete();
				}
				System.out.println("文件下载完毕,删除所有记录");
				Message msg = new Message();
				msg.what = DOWM_FIN;
				hander.sendMessage(msg);
			}
		}
	}

}


下面是xml的简单布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

 

    <EditText
        android:id="@+id/editText1"
        android:hint="请输入下载路径"
        android:text="http://192.168.191.1:9011/wireshark-win32-1.2.4.exe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下载" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下载进度" />

</LinearLayout>


下面要注意对sdcard进行读写操作要加权限,如下:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.INTERNET"/>
好了,所有代码已附录,新手上路多多关照,有繁琐逻辑的话,还请提出,下载应用程序,我已经试过了,没有问题,完全可以断点续传,对了,暂停的按钮没有添加,可以在eclipse里面进行暂停操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: