Skip to content

Commit 4f9682a

Browse files
committed
Homework 3rd
1 parent b20115e commit 4f9682a

File tree

10 files changed

+93
-54
lines changed

10 files changed

+93
-54
lines changed

group20/404130810/src/com/coderising/download/DownloadThread.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.coderising.download;
22

3+
import java.io.IOException;
4+
35
import com.coderising.download.api.Connection;
46

57
public class DownloadThread extends Thread{
@@ -15,6 +17,11 @@ public DownloadThread( Connection conn, int startPos, int endPos){
1517
this.endPos = endPos;
1618
}
1719
public void run(){
18-
20+
try {
21+
conn.read(startPos, endPos);
22+
} catch (IOException e) {
23+
// TODO Auto-generated catch block
24+
e.printStackTrace();
25+
}
1926
}
2027
}

group20/404130810/src/com/coderising/download/FileDownloader.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package com.coderising.download;
22

3+
import java.io.IOException;
4+
35
import com.coderising.download.api.Connection;
46
import com.coderising.download.api.ConnectionException;
57
import com.coderising.download.api.ConnectionManager;
68
import com.coderising.download.api.DownloadListener;
9+
import com.coderising.download.impl.ConnectionManagerImpl;
10+
import com.coderising.download.utils.FileDownloadUtil;
711

812
public class FileDownloader {
913

10-
String url;
14+
String url = "http://localhost:8080/MyServer/test.exe";
1115

1216
DownloadListener listener;
1317

1418
ConnectionManager cm;
1519

1620
public FileDownloader(String _url) {
1721
this.url = _url;
18-
22+
cm = new ConnectionManagerImpl();
1923
}
2024

21-
public void execute() {
25+
public void execute() throws IOException {
2226
// 在这里实现你的代码, 注意: 需要用多线程实现下载
2327
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码
2428
// (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos,
@@ -36,12 +40,16 @@ public void execute() {
3640
// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
3741
Connection conn = null;
3842
try {
39-
40-
conn = cm.open(this.url);
41-
43+
conn = cm.open(url);
4244
int length = conn.getContentLength();
43-
44-
new DownloadThread(conn, 0, length - 1).start();
45+
int[] posArr = FileDownloadUtil.generateDownloadPosArr(length);
46+
for (int i = 0; i < posArr.length; i++) {
47+
if(i == posArr.length - 1){
48+
new DownloadThread(cm.open(url), posArr[i], length).start();
49+
}else{
50+
new DownloadThread(cm.open(url), posArr[i], posArr[i + 1] - 1).start();
51+
}
52+
}
4553

4654
} catch (ConnectionException e) {
4755
e.printStackTrace();
@@ -65,4 +73,8 @@ public DownloadListener getListener() {
6573
return this.listener;
6674
}
6775

76+
public static void main(String[] args) throws IOException {
77+
new FileDownloader("http://localhost:8080/MyServer/Test.mp3").execute();
78+
}
79+
6880
}

group20/404130810/src/com/coderising/download/FileDownloaderTest.java

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

3+
import java.io.IOException;
4+
35
import org.junit.After;
46
import org.junit.Before;
57
import org.junit.Test;
@@ -19,7 +21,7 @@ public void tearDown() throws Exception {
1921
}
2022

2123
@Test
22-
public void testDownload() {
24+
public void testDownload() throws IOException {
2325

2426
String url = "http://localhost:8080/test.jpg";
2527

@@ -40,17 +42,17 @@ public void notifyFinished() {
4042

4143
downloader.execute();
4244

43-
// 等待多线程下载程序执行完毕
45+
// 等待多线程下载程序执行完毕
4446
while (!downloadFinished) {
4547
try {
46-
System.out.println("还没有下载完成,休眠五秒");
47-
//休眠5秒
48+
System.out.println("还没有下载完成,休眠五秒");
49+
//休眠5秒
4850
Thread.sleep(5000);
4951
} catch (InterruptedException e) {
5052
e.printStackTrace();
5153
}
5254
}
53-
System.out.println("下载完成!");
55+
System.out.println("下载完成!");
5456

5557

5658

group20/404130810/src/com/coderising/download/api/Connection.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44

55
public interface Connection {
66
/**
7-
* 给定开始和结束位置, 读取数据, 返回值是字节数组
8-
* @param startPos 开始位置, 从0开始
9-
* @param endPos 结束位置
7+
* 给定开始和结束位置, 读取数据, 返回值是字节数组
8+
* @param startPos 开始位置, 从0开始
9+
* @param endPos 结束位置
1010
* @return
1111
*/
1212
public byte[] read(int startPos,int endPos) throws IOException;
1313
/**
14-
* 得到数据内容的长度
14+
* 得到数据内容的长度
1515
* @return
1616
*/
1717
public int getContentLength();
1818

1919
/**
20-
* 关闭连接
20+
* 关闭连接
2121
*/
2222
public void close();
2323
}

group20/404130810/src/com/coderising/download/api/ConnectionException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
public class ConnectionException extends Exception {
44

5-
}
5+
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package com.coderising.download.api;
22

33
public interface ConnectionManager {
4-
/**
5-
* ¸ø¶¨Ò»¸öurl , ´ò¿ªÒ»¸öÁ¬½Ó
6-
* @param url
7-
* @return
8-
*/
9-
public Connection open(String url) throws ConnectionException;
10-
}
4+
public Connection open(String url) throws ConnectionException;
5+
}

group20/404130810/src/com/coderising/download/api/DownloadListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
public interface DownloadListener {
44
public void notifyFinished();
5-
}
5+
}
Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,49 @@
11
package com.coderising.download.impl;
22

33
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.RandomAccessFile;
6+
import java.net.HttpURLConnection;
7+
import java.net.URL;
48

59
import com.coderising.download.api.Connection;
610

7-
public class ConnectionImpl implements Connection{
11+
public class ConnectionImpl implements Connection {
812

9-
private byte[] fileContent;
13+
private HttpURLConnection httpConn;
14+
15+
public ConnectionImpl(String urlStr) {
16+
URL url;
17+
try {
18+
url = new URL(urlStr);
19+
httpConn = (HttpURLConnection) url.openConnection();
20+
} catch (IOException e) {
21+
e.printStackTrace();
22+
}
23+
}
1024

1125
@Override
1226
public byte[] read(int startPos, int endPos) throws IOException {
27+
int length = endPos - startPos;
28+
byte[] rtnByte = new byte[length];
1329

14-
return null;
30+
RandomAccessFile raf = new RandomAccessFile("test.mp3", "rw");
31+
raf.seek(startPos);
32+
InputStream is = httpConn.getInputStream();
33+
while ((length = is.read(rtnByte)) != -1) {
34+
raf.write(rtnByte, 0, length);
35+
}
36+
return rtnByte;
1537
}
1638

1739
@Override
1840
public int getContentLength() {
19-
return fileContent.length;
41+
return httpConn.getContentLength();
2042
}
2143

2244
@Override
2345
public void close() {
24-
25-
}
26-
27-
public void setFileContent(byte[] fileContent) {
28-
this.fileContent = fileContent;
46+
httpConn.disconnect();
2947
}
3048

3149
}
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
package com.coderising.download.impl;
22

3-
import java.io.IOException;
4-
import java.net.HttpURLConnection;
5-
import java.net.MalformedURLException;
6-
import java.net.URL;
7-
83
import com.coderising.download.api.Connection;
94
import com.coderising.download.api.ConnectionException;
105
import com.coderising.download.api.ConnectionManager;
116

127
public class ConnectionManagerImpl implements ConnectionManager {
13-
14-
158
@Override
169
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;
10+
Connection conn = new ConnectionImpl(urlStr);
11+
return conn;
2612
}
27-
2813
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.coderising.download.utils;
2+
3+
public class FileDownloadUtil {
4+
5+
public static int[] generateDownloadPosArr(int length){
6+
int[] posArr = new int[3];
7+
int firstPos = length/3;
8+
int secondPos = length/3 * 2;
9+
10+
posArr[0] = 0;
11+
posArr[1] = firstPos;
12+
posArr[2] = secondPos;
13+
14+
return posArr;
15+
}
16+
public static void main(String[] args) {
17+
FileDownloadUtil.generateDownloadPosArr(1000);
18+
}
19+
20+
}

0 commit comments

Comments
 (0)