Skip to content

Commit 96d4a55

Browse files
committed
Merge branch 'dev'
2 parents bf7313a + b20115e commit 96d4a55

File tree

9 files changed

+249
-0
lines changed

9 files changed

+249
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.coderising.download;
2+
3+
import com.coderising.download.api.Connection;
4+
5+
public class DownloadThread extends Thread{
6+
7+
Connection conn;
8+
int startPos;
9+
int endPos;
10+
11+
public DownloadThread( Connection conn, int startPos, int endPos){
12+
13+
this.conn = conn;
14+
this.startPos = startPos;
15+
this.endPos = endPos;
16+
}
17+
public void run(){
18+
19+
}
20+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.coderising.download;
2+
3+
import com.coderising.download.api.Connection;
4+
import com.coderising.download.api.ConnectionException;
5+
import com.coderising.download.api.ConnectionManager;
6+
import com.coderising.download.api.DownloadListener;
7+
8+
public class FileDownloader {
9+
10+
String url;
11+
12+
DownloadListener listener;
13+
14+
ConnectionManager cm;
15+
16+
public FileDownloader(String _url) {
17+
this.url = _url;
18+
19+
}
20+
21+
public void execute() {
22+
// 在这里实现你的代码, 注意: 需要用多线程实现下载
23+
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码
24+
// (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos,
25+
// endPos来指定)
26+
// (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有
27+
// 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。
28+
// 具体的实现思路:
29+
// 1. 需要调用ConnectionManager的open方法打开连接,
30+
// 然后通过Connection.getContentLength方法获得文件的长度
31+
// 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法
32+
// 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组
33+
// 3. 把byte数组写入到文件中
34+
// 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法
35+
36+
// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
37+
Connection conn = null;
38+
try {
39+
40+
conn = cm.open(this.url);
41+
42+
int length = conn.getContentLength();
43+
44+
new DownloadThread(conn, 0, length - 1).start();
45+
46+
} catch (ConnectionException e) {
47+
e.printStackTrace();
48+
} finally {
49+
if (conn != null) {
50+
conn.close();
51+
}
52+
}
53+
54+
}
55+
56+
public void setListener(DownloadListener listener) {
57+
this.listener = listener;
58+
}
59+
60+
public void setConnectionManager(ConnectionManager ucm) {
61+
this.cm = ucm;
62+
}
63+
64+
public DownloadListener getListener() {
65+
return this.listener;
66+
}
67+
68+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.coderising.download;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import com.coderising.download.api.ConnectionManager;
8+
import com.coderising.download.api.DownloadListener;
9+
import com.coderising.download.impl.ConnectionManagerImpl;
10+
11+
public class FileDownloaderTest {
12+
boolean downloadFinished = false;
13+
@Before
14+
public void setUp() throws Exception {
15+
}
16+
17+
@After
18+
public void tearDown() throws Exception {
19+
}
20+
21+
@Test
22+
public void testDownload() {
23+
24+
String url = "http://localhost:8080/test.jpg";
25+
26+
FileDownloader downloader = new FileDownloader(url);
27+
28+
29+
ConnectionManager cm = new ConnectionManagerImpl();
30+
downloader.setConnectionManager(cm);
31+
32+
downloader.setListener(new DownloadListener() {
33+
@Override
34+
public void notifyFinished() {
35+
downloadFinished = true;
36+
}
37+
38+
});
39+
40+
41+
downloader.execute();
42+
43+
// 等待多线程下载程序执行完毕
44+
while (!downloadFinished) {
45+
try {
46+
System.out.println("还没有下载完成,休眠五秒");
47+
//休眠5秒
48+
Thread.sleep(5000);
49+
} catch (InterruptedException e) {
50+
e.printStackTrace();
51+
}
52+
}
53+
System.out.println("下载完成!");
54+
55+
56+
57+
}
58+
59+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.coderising.download.api;
2+
3+
import java.io.IOException;
4+
5+
public interface Connection {
6+
/**
7+
* 给定开始和结束位置, 读取数据, 返回值是字节数组
8+
* @param startPos 开始位置, 从0开始
9+
* @param endPos 结束位置
10+
* @return
11+
*/
12+
public byte[] read(int startPos,int endPos) throws IOException;
13+
/**
14+
* 得到数据内容的长度
15+
* @return
16+
*/
17+
public int getContentLength();
18+
19+
/**
20+
* 关闭连接
21+
*/
22+
public void close();
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.coderising.download.api;
2+
3+
public class ConnectionException extends Exception {
4+
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.coderising.download.api;
2+
3+
public interface ConnectionManager {
4+
/**
5+
* ¸ø¶¨Ò»¸öurl , ´ò¿ªÒ»¸öÁ¬½Ó
6+
* @param url
7+
* @return
8+
*/
9+
public Connection open(String url) throws ConnectionException;
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.coderising.download.api;
2+
3+
public interface DownloadListener {
4+
public void notifyFinished();
5+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.coderising.download.impl;
2+
3+
import java.io.IOException;
4+
5+
import com.coderising.download.api.Connection;
6+
7+
public class ConnectionImpl implements Connection{
8+
9+
private byte[] fileContent;
10+
11+
@Override
12+
public byte[] read(int startPos, int endPos) throws IOException {
13+
14+
return null;
15+
}
16+
17+
@Override
18+
public int getContentLength() {
19+
return fileContent.length;
20+
}
21+
22+
@Override
23+
public void close() {
24+
25+
}
26+
27+
public void setFileContent(byte[] fileContent) {
28+
this.fileContent = fileContent;
29+
}
30+
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.coderising.download.impl;
2+
3+
import java.io.IOException;
4+
import java.net.HttpURLConnection;
5+
import java.net.MalformedURLException;
6+
import java.net.URL;
7+
8+
import com.coderising.download.api.Connection;
9+
import com.coderising.download.api.ConnectionException;
10+
import com.coderising.download.api.ConnectionManager;
11+
12+
public class ConnectionManagerImpl implements ConnectionManager {
13+
14+
15+
@Override
16+
public Connection open(String urlStr) throws ConnectionException {
17+
URL url;
18+
try {
19+
url = new URL(urlStr);
20+
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
21+
} catch (IOException e) {
22+
e.printStackTrace();
23+
}
24+
25+
return null;
26+
}
27+
28+
}

0 commit comments

Comments
 (0)