Skip to content

Commit b8ca828

Browse files
committed
Merge branch 'master' of github.com:honokaBiu/coding2017
2 parents 6ffbca8 + 961a6f6 commit b8ca828

27 files changed

+353
-22
lines changed

group20/592146505/coderising/src/org/wsc/array/ArrayUtil.java renamed to group20/592146505/592146505Learning/src/org/wsc/coderising/array/ArrayUtil.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.wsc.array;
1+
package org.wsc.coderising.array;
22

33
public class ArrayUtil {
44
/**

group20/592146505/coderising/src/org/wsc/array/ArrayUtilTest.java renamed to group20/592146505/592146505Learning/src/org/wsc/coderising/array/ArrayUtilTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.wsc.array;
1+
package org.wsc.coderising.array;
22

33
import static org.junit.Assert.*;
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.wsc.coderising.download;
2+
3+
import org.wsc.coderising.download.api.Connection;
4+
5+
/**
6+
* 下载进程
7+
*
8+
* @author Administrator
9+
* @date 2017年3月6日下午7:03:41
10+
* @version v1.0
11+
*
12+
*/
13+
public class DownloadThread extends Thread{
14+
15+
/** 连接 */
16+
Connection conn;
17+
/** 开始处 */
18+
int startPos;
19+
/** 结束处 */
20+
int endPos;
21+
22+
public DownloadThread( Connection conn, int startPos, int endPos){
23+
this.conn = conn;
24+
this.startPos = startPos;
25+
this.endPos = endPos;
26+
}
27+
public void run(){
28+
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.wsc.coderising.download;
2+
3+
import org.wsc.coderising.download.api.Connection;
4+
import org.wsc.coderising.download.api.ConnectionException;
5+
import org.wsc.coderising.download.api.ConnectionManager;
6+
import org.wsc.coderising.download.api.DownloadListener;
7+
8+
/**
9+
* 文件下载器
10+
*
11+
* @author Administrator
12+
* @date 2017年3月6日下午7:04:44
13+
* @version v1.0
14+
*
15+
*/
16+
public class FileDownloader {
17+
18+
String url;
19+
20+
DownloadListener listener;
21+
22+
ConnectionManager cm;
23+
24+
25+
public FileDownloader(String _url) {
26+
this.url = _url;
27+
28+
}
29+
30+
public void execute(){
31+
// 在这里实现你的代码, 注意: 需要用多线程实现下载
32+
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码
33+
// (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定)
34+
// (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有
35+
// 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。
36+
// 具体的实现思路:
37+
// 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度
38+
// 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法
39+
// 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组
40+
// 3. 把byte数组写入到文件中
41+
// 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法
42+
43+
// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
44+
Connection conn = null;
45+
try {
46+
47+
conn = cm.open(this.url);
48+
49+
int length = conn.getContentLength();
50+
51+
new DownloadThread(conn,0,length-1).start();
52+
53+
} catch (ConnectionException e) {
54+
e.printStackTrace();
55+
}finally{
56+
if(conn != null){
57+
conn.close();
58+
}
59+
}
60+
61+
62+
63+
64+
}
65+
66+
public void setListener(DownloadListener listener) {
67+
this.listener = listener;
68+
}
69+
70+
71+
72+
public void setConnectionManager(ConnectionManager ucm){
73+
this.cm = ucm;
74+
}
75+
76+
public DownloadListener getListener(){
77+
return this.listener;
78+
}
79+
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.wsc.coderising.download;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.wsc.coderising.download.api.ConnectionManager;
7+
import org.wsc.coderising.download.api.DownloadListener;
8+
import org.wsc.coderising.download.impl.ConnectionManagerImpl;
9+
10+
public class FileDownloaderTest {
11+
boolean downloadFinished = false;
12+
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+
ConnectionManager cm = new ConnectionManagerImpl();
29+
downloader.setConnectionManager(cm);
30+
// 生成回调函数
31+
downloader.setListener(new DownloadListener() {
32+
@Override
33+
public void notifyFinished() {
34+
downloadFinished = true;
35+
}
36+
37+
});
38+
// 开始下载
39+
downloader.execute();
40+
// 等待多线程下载程序执行完毕
41+
while (!downloadFinished) {
42+
try {
43+
System.out.println("还没有下载完成,休眠五秒");
44+
// 休眠5秒
45+
Thread.sleep(5000);
46+
} catch (InterruptedException e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
System.out.println("下载完成!");
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.wsc.coderising.download.api;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* 连接接口
7+
*
8+
* @author Administrator
9+
* @date 2017年3月6日下午7:00:53
10+
* @version v1.0
11+
*
12+
*/
13+
public interface Connection {
14+
/**
15+
* 给定开始和结束位置, 读取数据, 返回值是字节数组
16+
*
17+
* @param startPos
18+
* 开始位置, 从0开始
19+
* @param endPos
20+
* 结束位置
21+
* @return
22+
*/
23+
public byte[] read(int startPos, int endPos) throws IOException;
24+
25+
/**
26+
* 得到数据内容的长度
27+
*
28+
* @return
29+
*/
30+
public int getContentLength();
31+
32+
/**
33+
* 关闭连接
34+
*/
35+
public void close();
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.wsc.coderising.download.api;
2+
3+
/**
4+
*
5+
* 连接异常类
6+
*
7+
* @author Administrator
8+
* @date 2017年3月6日下午6:59:41
9+
* @version v1.0
10+
*
11+
*/
12+
public class ConnectionException extends Exception {
13+
14+
private static final long serialVersionUID = -249834831447340792L;
15+
16+
private ConnectionException() {
17+
super();
18+
}
19+
20+
private ConnectionException(String message, Throwable cause, boolean enableSuppression,
21+
boolean writableStackTrace) {
22+
super(message, cause, enableSuppression, writableStackTrace);
23+
}
24+
25+
private ConnectionException(String message, Throwable cause) {
26+
super(message, cause);
27+
}
28+
29+
private ConnectionException(String message) {
30+
super(message);
31+
}
32+
33+
private ConnectionException(Throwable cause) {
34+
super(cause);
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.wsc.coderising.download.api;
2+
3+
/**
4+
*
5+
* 管理连接接口
6+
*
7+
* @author Administrator
8+
* @date 2017年3月6日下午7:02:30
9+
* @version v1.0
10+
*
11+
*/
12+
public interface ConnectionManager {
13+
/**
14+
* 给定一个url , 打开一个连接
15+
*
16+
* @param url
17+
* @return
18+
*/
19+
public Connection open(String url) throws ConnectionException;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.wsc.coderising.download.api;
2+
3+
/**
4+
*
5+
* 下载监听接口
6+
*
7+
* @author Administrator
8+
* @date 2017年3月6日下午7:02:58
9+
* @version v1.0
10+
*
11+
*/
12+
public interface DownloadListener {
13+
/**
14+
* 通知下载完成回调函数
15+
*/
16+
public void notifyFinished();
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.wsc.coderising.download.impl;
2+
3+
import java.io.IOException;
4+
5+
import org.wsc.coderising.download.api.Connection;
6+
7+
/**
8+
*
9+
* 连接类
10+
*
11+
* @author Administrator
12+
* @date 2017年3月6日下午7:10:13
13+
* @version v1.0
14+
*
15+
*/
16+
public class ConnectionImpl implements Connection {
17+
18+
@Override
19+
public byte[] read(int startPos, int endPos) throws IOException {
20+
21+
return null;
22+
}
23+
24+
@Override
25+
public int getContentLength() {
26+
27+
return 0;
28+
}
29+
30+
@Override
31+
public void close() {
32+
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.wsc.coderising.download.impl;
2+
3+
import org.wsc.coderising.download.api.Connection;
4+
import org.wsc.coderising.download.api.ConnectionException;
5+
import org.wsc.coderising.download.api.ConnectionManager;
6+
7+
/**
8+
* 连接管理类
9+
*
10+
* @author Administrator
11+
* @date 2017年3月6日下午7:11:50
12+
* @version v1.0
13+
*
14+
*/
15+
public class ConnectionManagerImpl implements ConnectionManager {
16+
17+
@Override
18+
public Connection open(String url) throws ConnectionException {
19+
20+
return null;
21+
}
22+
23+
}

group20/592146505/coderising/src/org/wsc/litestruts/Action.java renamed to group20/592146505/592146505Learning/src/org/wsc/coderising/litestruts/Action.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.wsc.litestruts;
1+
package org.wsc.coderising.litestruts;
22

33
import java.util.Set;
44

group20/592146505/coderising/src/org/wsc/litestruts/LoginAction.java renamed to group20/592146505/592146505Learning/src/org/wsc/coderising/litestruts/LoginAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.wsc.litestruts;
1+
package org.wsc.coderising.litestruts;
22

33
/**
44
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。

group20/592146505/coderising/src/org/wsc/litestruts/Struts.java renamed to group20/592146505/592146505Learning/src/org/wsc/coderising/litestruts/Struts.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.wsc.litestruts;
1+
package org.wsc.coderising.litestruts;
22

33
import java.io.IOException;
44
import java.lang.reflect.Field;
@@ -14,7 +14,7 @@
1414
import org.w3c.dom.Element;
1515
import org.w3c.dom.Node;
1616
import org.w3c.dom.NodeList;
17-
import org.wsc.litestruts.util.DocumentUtil;
17+
import org.wsc.coderising.litestruts.util.DocumentUtil;
1818
import org.xml.sax.SAXException;
1919

2020
public class Struts {
@@ -24,7 +24,7 @@ public class Struts {
2424
/* 0. 读取配置文件struts.xml */
2525
DOCUMENT_UTIL = DocumentUtil.newInstance();
2626
try {
27-
document = DOCUMENT_UTIL.getDocument("src/struts.xml");
27+
document = DOCUMENT_UTIL.getDocument("src/org/wsc/litestruts/struts.xml");
2828
} catch (ParserConfigurationException | SAXException | IOException e) {
2929
e.printStackTrace();
3030
}

group20/592146505/coderising/src/org/wsc/litestruts/StrutsTest.java renamed to group20/592146505/592146505Learning/src/org/wsc/coderising/litestruts/StrutsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.wsc.litestruts;
1+
package org.wsc.coderising.litestruts;
22

33
import java.util.HashMap;
44
import java.util.Map;

group20/592146505/coderising/src/org/wsc/litestruts/View.java renamed to group20/592146505/592146505Learning/src/org/wsc/coderising/litestruts/View.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.wsc.litestruts;
1+
package org.wsc.coderising.litestruts;
22

33
import java.util.Map;
44

0 commit comments

Comments
 (0)