JAvaマルチスレッドブレークポイント再送
7559 ワード
android java ,
AndroidManifest.xml
<!-- internet -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- SDCard -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- SDCard -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
multiThreaddownload.java
package com.cn.download;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.URL;
import java.net.URLConnection;
import com.cn.coocaa.download.FileDownloadThread;
public class MultiThreadDownload extends Thread {
// ,
private static final int BUFFER_SIZE = 1024;
private int blockSize;
private int threadNum = 5;
private int fileSize;
private int downloadedSize;
String urlStr, threadNo, fileName;
private String savePath;
private int downloadPercent = 0 , downloadSpeed = 0, usedTime=0;
private long startTime,curTime;
private boolean completed = false;
// URL, , 。
public MultiThreadDownload(String URL, String savePath, String fileName) {
this.urlStr = URL;
this.savePath = savePath;
this.fileName = fileName;
}
@Override
public void run() {
FileDownloadThread[] fds = new FileDownloadThread[threadNum];
try {
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
fileSize = conn.getContentLength();
blockSize = fileSize / threadNum;
File file[] = new File[threadNum];
// , ,
for (int i = 0; i < threadNum; i++) {
file[i] = new File(savePath + fileName + ".part"
+ String.valueOf(i));
// , ,
FileDownloadThread fdt = new FileDownloadThread(url, file[i], i
* blockSize, (i + 1) != threadNum ? ((i + 1)
* blockSize - 1) : fileSize);
fdt.setName("Thread" + i);
fdt.start();
fds[i] = fdt;
}
startTime = System.currentTimeMillis();
// , 。
boolean finished = false;
while (!finished) {
downloadedSize = 0;
finished = true;
for (int i = 0; i < fds.length; i++) {
downloadedSize += fds[i].getDownloadSize();
if (!fds[i].isFinished()) {
finished = false;
}
}
//
downloadPercent = (downloadedSize*100)/fileSize;
// ,
curTime = System.currentTimeMillis();
usedTime =(int)((curTime-startTime)/1000);
if(usedTime==0)
usedTime =1;
downloadSpeed = (downloadedSize/usedTime)/1024;
sleep(1000);
}
//
completed = true;
//
RandomAccessFile raf = new RandomAccessFile(savePath + fileName,
"rw");
byte[] tempbytes = new byte[BUFFER_SIZE];
InputStream in = null;
int byteread = 0;
for (int i = 0; i < threadNum; i++) {
in = new FileInputStream(file[i]);
while ((byteread = in.read(tempbytes)) != -1)
{
raf.write(tempbytes, 0, byteread);
}
// 。
in.close();
file[i].delete();
}
raf.close();
} catch (Exception e) {
// TODO: handle exception
}
}
//
public int getDownloadPercent(){
return this.downloadPercent;
}
//
public int getDownloadSpeed(){
return this.downloadSpeed;
}
//
public void setThreadNum(int threadNum){
this.threadNum = threadNum;
}
//
public boolean isCompleted(){
return this.completed;
}
}
filedownloadthread.java
package com.cn.download;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URL;
import java.net.URLConnection;
public class FileDownloadThread extends Thread {
private static final int BUFFER_SIZE = 1024;
private URL url;
private File file;
private int startPosition;
private int endPosition;
private int curPosition;
private boolean finished = false;
private int downloadSize = 0;
//
public FileDownloadThread(URL url, File file, int startPosition,
int endPosition) {
this.url = url;
this.file = file;
this.startPosition = startPosition;
this.curPosition = startPosition;
this.endPosition = endPosition;
}
public void run() {
BufferedInputStream bis = null;
RandomAccessFile fos = null;
byte[] buf = new byte[BUFFER_SIZE];
URLConnection con = null;
try {
// URL
con = url.openConnection();
con.setAllowUserInteraction(true);
// , , 。
if ((file.length() + startPosition) == endPosition) {
this.finished = true;
}
// , , 。
else {
con.setRequestProperty("Range", "bytes="
+ (startPosition + file.length()) + "-" + endPosition);
fos = new RandomAccessFile(file, "rw");
fos.seek(file.length());
bis = new BufferedInputStream(con.getInputStream());
while (curPosition < endPosition) {
int len = bis.read(buf, 0, BUFFER_SIZE);
if (len == -1) {
break;
}
fos.write(buf, 0, len);
curPosition = curPosition + len;
if (curPosition > endPosition) {
downloadSize += len - (curPosition - endPosition) + 1;
} else {
downloadSize += len;
}
}
this.finished = true;
bis.close();
fos.close();
}
} catch (IOException e) {
System.out.println(getName() + " Error:" + e.getMessage());
}
}
public boolean isFinished() {
return finished;
}
public int getDownloadSize() {
return downloadSize;
}
}
。
, 。
:
1、
2、 。
3、 , , 。
4、 。
5、 , 。
6、 , 。
, , , , ,
, 。
:http://alloxa.blog.51cto.com/