Skip to content

Commit a9c2dd5

Browse files
committed
240094626 work0315 download
1 parent 0e47f49 commit a9c2dd5

File tree

5 files changed

+109
-100
lines changed

5 files changed

+109
-100
lines changed
Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,51 @@
11
package com.coderising.download;
22

3-
import java.io.File;
4-
import java.io.FileOutputStream;
5-
import java.io.OutputStream;
3+
import java.io.RandomAccessFile;
4+
import java.util.concurrent.CyclicBarrier;
65

76
import com.coderising.download.api.Connection;
87

98
public class DownloadThread extends Thread{
109

11-
Connection conn;
12-
int startPos;
13-
int endPos;
14-
int threadId;
15-
private static final int BUFF_LENGTH = 1024;
16-
private final static String TEMP_FILE_PATH = "E:/temp";
17-
String filePath ;
10+
private Connection conn;
11+
private int startPos;
12+
private int endPos;
13+
private int threadId;
14+
private String localFile;
15+
private static final int BUFF_LENGTH = 1024 * 4;
1816
boolean isFinished = false;
19-
long currSize = 0;
17+
int currSize = 0;
18+
CyclicBarrier barrier;
2019

21-
public DownloadThread( Connection conn, int startPos, int endPos,int threadId){
20+
public DownloadThread( Connection conn, int startPos, int endPos,int threadId,String localFile,CyclicBarrier barrier){
2221

2322
this.conn = conn;
2423
this.startPos = startPos;
2524
this.endPos = endPos;
2625
this.threadId = threadId;
27-
filePath = TEMP_FILE_PATH + "/"+threadId+".tmp";
26+
this.localFile = localFile;
27+
this.barrier = barrier;
2828
}
2929
public void run(){
3030
try {
31-
// create temp file with threadId
32-
createTmepFile();
33-
34-
// read temp file
35-
writeTempFile();
36-
31+
System.out.println("Thread"+threadId+" begin download bytes range:"+startPos+"-"+endPos);
32+
RandomAccessFile raf = new RandomAccessFile(localFile, "rw");
33+
int totalLen = endPos - startPos + 1;
34+
while(currSize < totalLen){
35+
int start = currSize + startPos;
36+
int end = start + BUFF_LENGTH-1;
37+
byte[] data = conn.read(start,(end>endPos?endPos:end));
38+
39+
raf.seek(start);
40+
raf.write(data);
41+
currSize += data.length;
42+
43+
44+
}
45+
raf.close();
46+
barrier.await(); //等待别的线程完成
3747
} catch (Exception e) {
3848
e.printStackTrace();
3949
}
4050
}
41-
private void createTmepFile() {
42-
//判断路径temp是否存在,不存在则先创建
43-
File tempDir = new File(TEMP_FILE_PATH);
44-
if(!tempDir.exists() || !tempDir.isDirectory()){
45-
tempDir.mkdir();
46-
}
47-
48-
File file = new File(filePath);
49-
if(file.exists()){
50-
currSize = file.length();
51-
}else{
52-
currSize = 0;
53-
}
54-
}
55-
private int writeTempFile() {
56-
int size = 0;
57-
OutputStream fout = null;
58-
try{
59-
fout = new FileOutputStream(filePath, true);
60-
for(int i = 0; i < sbLogMsg.size(); i++){
61-
StringBuffer logMsg = sbLogMsg.get(i);
62-
byte[] tmpBytes = CommUtil.StringToBytes(logMsg.toString());
63-
fout.write(tmpBytes);
64-
size += tmpBytes.length;
65-
}
66-
}catch(Exception e){
67-
e.printStackTrace();
68-
}finally{
69-
if(fout != null){
70-
fout.close();
71-
}
72-
}
73-
return size;
74-
75-
}
7651
}

group17/240094626/work_0305/src/com/coderising/download/FileDownloader.java

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
package com.coderising.download;
22

3+
import java.io.IOException;
4+
import java.io.RandomAccessFile;
5+
import java.util.concurrent.CyclicBarrier;
6+
37
import com.coderising.download.api.Connection;
4-
import com.coderising.download.api.ConnectionException;
58
import com.coderising.download.api.ConnectionManager;
69
import com.coderising.download.api.DownloadListener;
710

811

912
public class FileDownloader {
1013

11-
String url;
14+
private String url;
15+
16+
private DownloadListener listener;
1217

13-
DownloadListener listener;
18+
private ConnectionManager cm;
1419

15-
ConnectionManager cm;
20+
private String localFile;
1621

17-
int threadNum;
22+
private int threadNum;
1823

1924
//一组开始下载位置
2025
private int[] startPos;
2126
//一组结束下载位置
2227
private int[] endPos;
2328

24-
private boolean stop = false;
25-
26-
27-
public FileDownloader(String _url,int threadNum) {
29+
30+
public FileDownloader(String _url,int threadNum,String localFile) {
2831
this.url = _url;
2932
this.threadNum = threadNum;
33+
this.localFile = localFile;
34+
startPos = new int[threadNum];
35+
endPos = new int[threadNum];
3036

3137
}
3238

@@ -44,13 +50,24 @@ public void execute(){
4450
// 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法
4551

4652
// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
53+
54+
CyclicBarrier barrier = new CyclicBarrier(threadNum, new Runnable() {
55+
@Override
56+
public void run() {
57+
listener.notifyFinished();
58+
}
59+
});
60+
4761
Connection conn = null;
4862
try {
4963

5064
conn = cm.open(this.url);
5165

5266
int length = conn.getContentLength();
67+
5368
if(length > 0){
69+
holderFile(localFile,length);
70+
5471
for(int i = 0 , len = length/threadNum; i < threadNum; i++){
5572
int size = i * len;
5673
startPos[i] = size;
@@ -59,14 +76,18 @@ public void execute(){
5976
}else{
6077
endPos[i] = size + len-1;
6178
}
62-
Connection downConn = cm.open(url);
63-
new DownloadThread(downConn, startPos[i], endPos[i], i+1).start();
79+
new DownloadThread(cm.open(url),
80+
startPos[i],
81+
endPos[i],
82+
i+1,
83+
localFile,
84+
barrier).start();
6485
}
6586

6687
}
6788

6889

69-
} catch (ConnectionException e) {
90+
} catch (Exception e) {
7091
e.printStackTrace();
7192
}finally{
7293
if(conn != null){
@@ -79,6 +100,14 @@ public void execute(){
79100

80101
}
81102

103+
private void holderFile(String localFile, int length) throws IOException {
104+
RandomAccessFile raf = new RandomAccessFile(localFile, "rw");
105+
for(int i = 0; i < length; i++){
106+
raf.write(0);
107+
}
108+
raf.close();
109+
}
110+
82111
public void setListener(DownloadListener listener) {
83112
this.listener = listener;
84113
}

group17/240094626/work_0305/src/com/coderising/download/FileDownloaderTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ public void tearDown() throws Exception {
2121
@Test
2222
public void testDownload() {
2323

24-
String url = "http://localhost:8080/test.jpg";
24+
String url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png";
25+
String localFile = "E:\\Users\\2017coding\\temp\\pic.png";
2526
int threadNum = Runtime.getRuntime().availableProcessors();
26-
FileDownloader downloader = new FileDownloader(url,threadNum);
27+
FileDownloader downloader = new FileDownloader(url,threadNum,localFile);
2728

2829

2930
ConnectionManager cm = new ConnectionManagerImpl();

group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionImpl.java

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,62 @@
11
package com.coderising.download.impl;
22

3+
import java.io.IOException;
34
import java.io.InputStream;
45
import java.net.HttpURLConnection;
6+
import java.net.MalformedURLException;
7+
import java.net.URL;
58

69
import com.coderising.download.api.Connection;
10+
import com.coderising.download.api.ConnectionException;
711

8-
public class ConnectionImpl implements Connection{
12+
class ConnectionImpl implements Connection{
913

10-
private static final int BUFF_LENGTH = 1024;
14+
private static final int BUFF_SIZE = 1024;
1115

12-
private HttpURLConnection conn;
16+
private URL url;
1317

14-
public ConnectionImpl(HttpURLConnection conn) {
15-
this.conn = conn;
18+
ConnectionImpl(String _url) throws ConnectionException{
19+
try {
20+
this.url = new URL(_url);
21+
} catch (MalformedURLException e) {
22+
throw new ConnectionException(e);
23+
}
1624
}
1725

1826
@Override
1927
public byte[] read(int startPos, int endPos) throws Exception {
2028
if(startPos > endPos){
2129
throw new IllegalArgumentException("startPos:"+startPos+",endPos:"+endPos);
2230
}
31+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
2332
String property = "bytes="+startPos+"-"+endPos;
2433
conn.setRequestProperty("RANGE", property);
25-
if(conn.getResponseCode() == 206){
26-
InputStream is = conn.getInputStream();
27-
byte[] bytes = new byte[endPos-startPos];
28-
byte[] buff = new byte[BUFF_LENGTH];
29-
int len,i=0;
30-
while((len = is.read(buff) ) != 0){
31-
System.arraycopy(buff, 0, bytes, i*len, len);
32-
i++;
33-
}
34-
return bytes;
34+
conn.connect();
35+
int totalLen = endPos - startPos + 1;
36+
InputStream is = conn.getInputStream();
37+
byte[] bytes = new byte[totalLen];
38+
byte[] buff = new byte[BUFF_SIZE];
39+
int len,
40+
count = 0;
41+
while((len = is.read(buff) ) > 0 && count < totalLen){
42+
43+
System.arraycopy(buff, 0, bytes,count, len);
44+
count += len;
3545
}
36-
return null;
46+
return bytes;
3747
}
3848

3949
@Override
4050
public int getContentLength() {
41-
return conn.getContentLength();
51+
HttpURLConnection conn;
52+
try {
53+
conn = (HttpURLConnection) url.openConnection();
54+
return conn.getContentLength();
55+
} catch (IOException e) {
56+
e.printStackTrace();
57+
}
58+
59+
return -1;
4260
}
4361

4462
@Override
Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.coderising.download.impl;
22

3-
import java.net.HttpURLConnection;
4-
import java.net.URL;
5-
63
import com.coderising.download.api.Connection;
74
import com.coderising.download.api.ConnectionException;
85
import com.coderising.download.api.ConnectionManager;
@@ -11,18 +8,7 @@ public class ConnectionManagerImpl implements ConnectionManager {
118

129
@Override
1310
public Connection open(String url) throws ConnectionException {
14-
Connection conn = null;
15-
try {
16-
URL u = new URL(url);
17-
HttpURLConnection hConn = (HttpURLConnection) u.openConnection();
18-
hConn.setConnectTimeout(5000);
19-
hConn.setReadTimeout(5000);
20-
hConn.setRequestMethod("GET");
21-
conn = new ConnectionImpl(hConn);
22-
} catch (Exception e) {
23-
throw new ConnectionException("连接打开失败:"+url, e);
24-
}
25-
return conn;
11+
return new ConnectionImpl(url);
2612
}
2713

2814
}

0 commit comments

Comments
 (0)