-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from ChristopherYue/master
group 10
- Loading branch information
Showing
30 changed files
with
743 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="ISO-8859-1"?> | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
<web-app xmlns="http://java.sun.com/xml/ns/javaee" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | ||
version="2.5"> | ||
|
||
<display-name>Welcome to Tomcat</display-name> | ||
<description> | ||
Welcome to Tomcat | ||
</description> | ||
|
||
</web-app> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
group10/3314793852/second/.settings/org.eclipse.core.resources.prefs
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
group10/3314793852/second/.settings/org.eclipse.jdt.core.prefs
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
50 changes: 50 additions & 0 deletions
50
group10/3314793852/src/com/coderising/download/DownloadThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 打开输入流下载,对应分配的资源。 | ||
*/ | ||
package com.coderising.download; | ||
|
||
import java.io.IOException; | ||
import java.io.RandomAccessFile; | ||
|
||
import com.coderising.download.api.Connection; | ||
|
||
public class DownloadThread extends Thread{ | ||
|
||
Connection conn; //用来打开下载资源的对用的输入流。 | ||
RandomAccessFile currentPart; //当前线程下载到本地的位置 | ||
int startPos; //下载起始位置。 | ||
int endPos; //下载结束位置。 | ||
int hasRead=0; //已经下载的长度。 | ||
|
||
//构造器。 | ||
public DownloadThread( Connection conn, int startPos, int endPos,RandomAccessFile currentPart){ | ||
|
||
this.conn = conn; | ||
this.currentPart=currentPart; | ||
this.startPos = startPos; | ||
this.endPos = endPos; | ||
} | ||
|
||
|
||
/*在该方法中要将数据从byte数组写入到文件中. | ||
* 要先把数据写入byte数组中,再从数组中把数据写入文件中。 | ||
*/ | ||
public void run(){ | ||
|
||
try { | ||
synchronized(this){ | ||
byte[] buff=new byte[endPos-startPos+1]; | ||
buff=conn.read(startPos, endPos); | ||
//将字节数组中的数据读入指定文件中。 | ||
for(int i=0;i<buff.length;i++){ | ||
currentPart.write(buff[i]); | ||
hasRead++; | ||
} | ||
//System.out.println("Over"); | ||
currentPart.close(); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
group10/3314793852/src/com/coderising/download/FileDownloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
|
||
package com.coderising.download; | ||
|
||
import java.io.RandomAccessFile; | ||
|
||
import com.coderising.download.api.Connection; | ||
import com.coderising.download.api.ConnectionException; | ||
import com.coderising.download.api.ConnectionManager; | ||
import com.coderising.download.api.DownloadListener; | ||
import com.coderising.download.impl.DownloadListenerImpl; | ||
|
||
|
||
public class FileDownloader { | ||
|
||
String url; //URL地址 | ||
DownloadListener listener; //下载完成通知 | ||
ConnectionManager cm; //打开URL链接 | ||
String targetFile="E:/moon.png"; //指定所下载文件的保存位置 | ||
|
||
public FileDownloader(String _url) { | ||
this.url = _url; | ||
} | ||
|
||
public void execute(){ | ||
// 在这里实现你的代码, 注意: 需要用多线程实现下载 | ||
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 | ||
// (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) | ||
// (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 | ||
// 线程都执行完以后, 调用DownloadListener的notifiedFinished方法, 这样客户端就能收到通知。 | ||
// 具体的实现思路: | ||
// 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 | ||
// 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 | ||
// 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 | ||
// 3. 把byte数组写入到文件中 | ||
// 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 | ||
|
||
// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 | ||
Connection conn = null; | ||
try { | ||
|
||
conn = cm.open(this.url); //ConnectionManager的对象的open方法返回一个Connection对象 | ||
int length = conn.getContentLength(); //获得文件的总长度。 | ||
//System.out.println(length); | ||
//创建一个文件,用来保存下载的资源。 | ||
RandomAccessFile file = new RandomAccessFile(targetFile, "rw"); | ||
//设置本地文件的大小。 | ||
file.setLength(length); | ||
|
||
file.close(); | ||
|
||
|
||
int numberOfThread=4; //线程的个数,自定义,当前定为4. | ||
|
||
int eachLength=length/4+1; //每个线程的下载的长度。 | ||
RandomAccessFile[] currentPart=new RandomAccessFile[4]; | ||
DownloadThread[] thread=new DownloadThread[4]; | ||
for(int i=0;i<numberOfThread-1;i++){ //确定子线程的开始和结尾点,并启动子线程。 | ||
currentPart[i]= new RandomAccessFile(targetFile, "rw"); | ||
currentPart[i].seek(0+eachLength*i); | ||
thread[i]=new DownloadThread(conn,0+eachLength*i,eachLength+eachLength*i-1,currentPart[i]); | ||
thread[i].start(); //启动线程。 | ||
//System.out.println(i); | ||
} | ||
currentPart[3]= new RandomAccessFile(targetFile, "rw"); | ||
currentPart[3].seek(eachLength*3); | ||
thread[3]=new DownloadThread(conn,eachLength*3,length-1,currentPart[3]); | ||
thread[3].start(); //最后一个线程 | ||
|
||
//统计多条线程已经下载的总大小。 | ||
|
||
|
||
|
||
int sumSize=0; | ||
//检测是否下载完毕 | ||
while(true){ | ||
|
||
for(int j=0;j<numberOfThread;j++){ | ||
sumSize+=thread[j].hasRead; | ||
} | ||
//System.out.println("sumSize"+sumSize); | ||
if(sumSize==length){ | ||
listener.notifyFinished(); | ||
//System.out.println("完成"); | ||
System.out.println("下载完成!"); | ||
break; | ||
} | ||
sumSize=0; | ||
// Thread.sleep(10000); | ||
} | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
}finally{ | ||
// if(conn != null){ | ||
// conn.close(); | ||
// } | ||
} | ||
} | ||
|
||
|
||
|
||
public void setListener(DownloadListener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
|
||
|
||
public void setConnectionManager(ConnectionManager ucm){ | ||
this.cm = ucm; | ||
} | ||
|
||
public DownloadListener getListener(){ | ||
return this.listener; | ||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
group10/3314793852/src/com/coderising/download/FileDownloaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
|
||
package com.coderising.download; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.coderising.download.api.ConnectionManager; | ||
import com.coderising.download.api.DownloadListener; | ||
import com.coderising.download.impl.ConnectionManagerImpl; | ||
import com.coderising.download.impl.DownloadListenerImpl; | ||
|
||
public class FileDownloaderTest { | ||
boolean downloadFinished = false; | ||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testDownload() { | ||
|
||
String url = "http://localhost:8080/demo/moon.png"; | ||
|
||
FileDownloader downloader = new FileDownloader(url); | ||
|
||
|
||
ConnectionManager cm = new ConnectionManagerImpl(); | ||
downloader.setConnectionManager(cm); | ||
|
||
|
||
downloader.setListener(new DownloadListener() { | ||
@Override | ||
public void notifyFinished() { | ||
downloadFinished = true; | ||
} | ||
|
||
}); | ||
|
||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
while(!downloadFinished){ | ||
System.out.println("还没有下载完成,休眠五秒"); | ||
try { | ||
Thread.sleep(5000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
System.out.println("下载完成!"); | ||
} | ||
}).start(); | ||
|
||
downloader.execute(); | ||
|
||
|
||
|
||
|
||
// 等待多线程下载程序执行完毕 | ||
|
||
// while (!downloadFinished) { | ||
// try { | ||
// System.out.println("还没有下载完成,休眠五秒"); | ||
// //休眠5秒 | ||
// Thread.sleep(5000); | ||
// } catch (InterruptedException e) { | ||
// e.printStackTrace(); | ||
// } | ||
// } | ||
// System.out.println("下载完成!"); | ||
|
||
|
||
|
||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
group10/3314793852/src/com/coderising/download/api/Connection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.coderising.download.api; | ||
|
||
import java.io.IOException; | ||
|
||
public interface Connection { | ||
/** | ||
* 给定开始和结束位置, 读取数据, 返回值是字节数组 | ||
* @param startPos 开始位置, 从0开始 | ||
* @param endPos 结束位置 | ||
* @return | ||
*/ | ||
|
||
|
||
public byte[] read(int startPos,int endPos) throws IOException; | ||
/** | ||
* 得到数据内容的长度 | ||
* @return | ||
*/ | ||
public int getContentLength(); | ||
|
||
/** | ||
* 关闭连接 | ||
*/ | ||
public void close(); | ||
} |
5 changes: 5 additions & 0 deletions
5
group10/3314793852/src/com/coderising/download/api/ConnectionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.coderising.download.api; | ||
|
||
public class ConnectionException extends Exception { | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
group10/3314793852/src/com/coderising/download/api/ConnectionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.coderising.download.api; | ||
|
||
public interface ConnectionManager { | ||
/** | ||
* 给定一个url , 打开一个连接 | ||
* @param url | ||
* @return | ||
*/ | ||
public Connection open(String url) throws ConnectionException; | ||
} |
5 changes: 5 additions & 0 deletions
5
group10/3314793852/src/com/coderising/download/api/DownloadListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.coderising.download.api; | ||
|
||
public interface DownloadListener { | ||
public void notifyFinished(); | ||
} |
Oops, something went wrong.