Skip to content

Commit 3cdc5f3

Browse files
authored
Merge pull request #28 from luojunyi/master
第三周 luojunyi 191191717
2 parents 5d785fe + 012bc6e commit 3cdc5f3

File tree

9 files changed

+437
-0
lines changed

9 files changed

+437
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package week3.com.coding.download;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.RandomAccessFile;
6+
7+
import week3.com.coding.download.api.Connection;
8+
9+
public class DownloadThread extends Thread
10+
{
11+
12+
Connection conn;
13+
14+
int startPos;
15+
16+
int endPos;
17+
18+
public DownloadThread(Connection conn, int startPos, int endPos)
19+
{
20+
21+
this.conn = conn;
22+
this.startPos = startPos;
23+
this.endPos = endPos;
24+
}
25+
26+
/**
27+
* 线程执行方法,启动线程读取一定长度的字节,并写到文件中
28+
*/
29+
public void run()
30+
{
31+
File f = new File("d:\\test.txt");
32+
RandomAccessFile raf = null;
33+
try
34+
{
35+
raf = new RandomAccessFile(f, "rwd");
36+
raf.seek(startPos);// 定位当前的指针
37+
// raf.close();
38+
byte[] bs = conn.read(startPos, endPos);
39+
raf.write(bs);
40+
}
41+
catch (IOException e)
42+
{
43+
e.printStackTrace();
44+
}
45+
}
46+
47+
/**
48+
* 资源释放
49+
*
50+
* @param raf
51+
* @param conn
52+
*/
53+
public void release(RandomAccessFile raf, Connection conn)
54+
{
55+
try
56+
{
57+
raf.close();
58+
}
59+
catch (IOException e)
60+
{
61+
e.printStackTrace();
62+
}
63+
conn.close();
64+
}
65+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package week3.com.coding.download;
2+
3+
import java.io.IOException;
4+
5+
import week3.com.coding.download.api.Connection;
6+
import week3.com.coding.download.api.ConnectionException;
7+
import week3.com.coding.download.api.ConnectionManager;
8+
import week3.com.coding.download.api.DownloadListener;
9+
import week3.com.coding.download.impl.ConnectionImpl;
10+
import week3.com.coding.download.impl.ConnectionManagerImpl;
11+
12+
public class FileDownloader
13+
{
14+
15+
String url;
16+
17+
DownloadListener listener;
18+
19+
ConnectionManager cm;
20+
21+
int ThreadNum;
22+
23+
public FileDownloader(String url, int threadNum)
24+
{
25+
super();
26+
this.url = url;
27+
ThreadNum = threadNum;
28+
}
29+
30+
public void execute()
31+
{
32+
// 在这里实现你的代码, 注意: 需要用多线程实现下载
33+
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码
34+
// (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定)
35+
// (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有
36+
// 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。
37+
// 具体的实现思路:
38+
// 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度
39+
// 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法
40+
// 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组
41+
// 3. 把byte数组写入到文件中
42+
// 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法
43+
44+
// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
45+
Connection conn = null;
46+
try
47+
{
48+
conn = (ConnectionImpl)cm.open(this.url);
49+
int length = conn.getContentLength();// 获取文件的长度
50+
// 三个线程,每个线程下载长度要平均
51+
int blockSize = length / this.ThreadNum;
52+
for (int i = 1; i <= this.ThreadNum; i++)
53+
{
54+
int sPos = (i - 1) * blockSize;
55+
int ePos = i * blockSize - 1;
56+
// 如果是最后一个,则结束位置等于最后的地方
57+
if (i == this.ThreadNum)
58+
{
59+
ePos = length;
60+
}
61+
new DownloadThread(conn, sPos, ePos).start();
62+
}
63+
}
64+
catch (ConnectionException e)
65+
{
66+
e.printStackTrace();
67+
}
68+
finally
69+
{
70+
if (conn != null)
71+
{
72+
conn.close();
73+
}
74+
}
75+
76+
}
77+
78+
public void setListener(DownloadListener listener)
79+
{
80+
this.listener = listener;
81+
}
82+
83+
public void setConnectionManager(ConnectionManager ucm)
84+
{
85+
this.cm = ucm;
86+
}
87+
88+
public DownloadListener getListener()
89+
{
90+
return this.listener;
91+
}
92+
93+
public String getUrl()
94+
{
95+
return url;
96+
}
97+
98+
public void setUrl(String url)
99+
{
100+
this.url = url;
101+
}
102+
103+
public ConnectionManager getCm()
104+
{
105+
return cm;
106+
}
107+
108+
public void setCm(ConnectionManager cm)
109+
{
110+
this.cm = cm;
111+
}
112+
113+
public int getThreadNum()
114+
{
115+
return ThreadNum;
116+
}
117+
118+
public void setThreadNum(int threadNum)
119+
{
120+
ThreadNum = threadNum;
121+
}
122+
123+
public static void main(String[] args)
124+
throws ConnectionException, IOException
125+
{
126+
127+
String url = "http://localhost:8088/JSPDemo/test.txt";
128+
// ConnectionImpl ci=(ConnectionImpl)cm.open(url);
129+
// System.out.println(new String(ci.read(2, 31)));
130+
// File f = new File("d:\\test.txt");
131+
// RandomAccessFile raf = new RandomAccessFile(f, "rwd");
132+
// raf.seek(raf.length());// 定位当前的指针
133+
134+
FileDownloader downloader = new FileDownloader(url,3);
135+
downloader.setConnectionManager(new ConnectionManagerImpl());
136+
downloader.execute();
137+
// int length = conn.getContentLength();// 获取文件的长度
138+
// System.out.println("urlConn: " + length);
139+
// int blockSize = length / 3;
140+
141+
// new DownloadThread(conn, 0, blockSize - 1).start();// 第一个线程
142+
// new DownloadThread(conn, blockSize, blockSize * 2 - 1).start();// 第二个线程
143+
// new DownloadThread(conn, blockSize * 2 , length - 1).start();// 第三个线程
144+
}
145+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package week3.com.coding.download;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import week3.com.coding.download.api.ConnectionManager;
8+
import week3.com.coding.download.api.DownloadListener;
9+
import week3.com.coding.download.impl.ConnectionManagerImpl;
10+
11+
public class FileDownloaderTest
12+
{
13+
boolean downloadFinished = false;
14+
15+
@Before
16+
public void setUp()
17+
throws Exception
18+
{
19+
}
20+
21+
@After
22+
public void tearDown()
23+
throws Exception
24+
{
25+
}
26+
27+
@Test
28+
public void testDownload()
29+
{
30+
String url = "http://localhost:8088/JSPDemo/test.txt";
31+
32+
FileDownloader downloader = new FileDownloader(url,3);
33+
ConnectionManager cm = new ConnectionManagerImpl();
34+
downloader.setConnectionManager(cm);
35+
36+
downloader.setListener(new DownloadListener()
37+
{
38+
@Override
39+
public void notifyFinished()
40+
{
41+
downloadFinished = true;
42+
}
43+
44+
});
45+
46+
downloader.execute();
47+
48+
// 等待多线程下载程序执行完毕
49+
while (!downloadFinished)
50+
{
51+
try
52+
{
53+
System.out.println("还没有下载完成,休眠五秒");
54+
// 休眠5秒
55+
Thread.sleep(5000);
56+
}
57+
catch (InterruptedException e)
58+
{
59+
e.printStackTrace();
60+
}
61+
}
62+
System.out.println("下载完成!");
63+
64+
}
65+
66+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package week3.com.coding.download.api;
2+
3+
import java.io.IOException;
4+
5+
public interface Connection
6+
{
7+
/**
8+
* 给定开始和结束位置, 读取数据, 返回值是字节数组
9+
*
10+
* @param startPos 开始位置, 从0开始
11+
* @param endPos 结束位置
12+
* @return
13+
*/
14+
public byte[] read(int startPos, int endPos)
15+
throws IOException;
16+
17+
/**
18+
* 得到数据内容的长度
19+
*
20+
* @return
21+
*/
22+
public int getContentLength();
23+
24+
/**
25+
* 关闭连接
26+
*/
27+
public void close();
28+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package week3.com.coding.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 week3.com.coding.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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package week3.com.coding.download.api;
2+
3+
public interface DownloadListener
4+
{
5+
public void notifyFinished();
6+
}

0 commit comments

Comments
 (0)