Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
conf1102 committed Mar 10, 2017
2 parents 96d4a55 + 4f9682a commit b8c763d
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.coderising.download;

import java.io.IOException;

import com.coderising.download.api.Connection;

public class DownloadThread extends Thread{
Expand All @@ -15,6 +17,11 @@ public DownloadThread( Connection conn, int startPos, int endPos){
this.endPos = endPos;
}
public void run(){

try {
conn.read(startPos, endPos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
28 changes: 20 additions & 8 deletions group20/404130810/src/com/coderising/download/FileDownloader.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package com.coderising.download;

import java.io.IOException;

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.ConnectionManagerImpl;
import com.coderising.download.utils.FileDownloadUtil;

public class FileDownloader {

String url;
String url = "http://localhost:8080/MyServer/test.exe";

DownloadListener listener;

ConnectionManager cm;

public FileDownloader(String _url) {
this.url = _url;

cm = new ConnectionManagerImpl();
}

public void execute() {
public void execute() throws IOException {
// 在这里实现你的代码, 注意: 需要用多线程实现下载
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码
// (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos,
Expand All @@ -36,12 +40,16 @@ public void execute() {
// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
Connection conn = null;
try {

conn = cm.open(this.url);

conn = cm.open(url);
int length = conn.getContentLength();

new DownloadThread(conn, 0, length - 1).start();
int[] posArr = FileDownloadUtil.generateDownloadPosArr(length);
for (int i = 0; i < posArr.length; i++) {
if(i == posArr.length - 1){
new DownloadThread(cm.open(url), posArr[i], length).start();
}else{
new DownloadThread(cm.open(url), posArr[i], posArr[i + 1] - 1).start();
}
}

} catch (ConnectionException e) {
e.printStackTrace();
Expand All @@ -65,4 +73,8 @@ public DownloadListener getListener() {
return this.listener;
}

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

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.coderising.download;

import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -19,7 +21,7 @@ public void tearDown() throws Exception {
}

@Test
public void testDownload() {
public void testDownload() throws IOException {

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

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

downloader.execute();

// 等待多线程下载程序执行完毕
// 等待多线程下载程序执行完毕
while (!downloadFinished) {
try {
System.out.println("还没有下载完成,休眠五秒");
//休眠5秒
System.out.println("还没有下载完成,休眠五秒");
//休眠5秒
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("下载完成!");
System.out.println("下载完成!");



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

public interface Connection {
/**
* 给定开始和结束位置, 读取数据, 返回值是字节数组
* @param startPos 开始位置, 从0开始
* @param endPos 结束位置
* 给定开始和结束位置, 读取数据, 返回值是字节数组
* @param startPos 开始位置, 从0开始
* @param endPos 结束位置
* @return
*/
public byte[] read(int startPos,int endPos) throws IOException;
/**
* 得到数据内容的长度
* 得到数据内容的长度
* @return
*/
public int getContentLength();

/**
* 关闭连接
* 关闭连接
*/
public void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

public class ConnectionException extends Exception {

}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package com.coderising.download.api;

public interface ConnectionManager {
/**
* ¸ø¶¨Ò»¸öurl , ´ò¿ªÒ»¸öÁ¬½Ó
* @param url
* @return
*/
public Connection open(String url) throws ConnectionException;
}
public Connection open(String url) throws ConnectionException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

public interface DownloadListener {
public void notifyFinished();
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
package com.coderising.download.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

import com.coderising.download.api.Connection;

public class ConnectionImpl implements Connection{
public class ConnectionImpl implements Connection {

private byte[] fileContent;
private HttpURLConnection httpConn;

public ConnectionImpl(String urlStr) {
URL url;
try {
url = new URL(urlStr);
httpConn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public byte[] read(int startPos, int endPos) throws IOException {
int length = endPos - startPos;
byte[] rtnByte = new byte[length];

return null;
RandomAccessFile raf = new RandomAccessFile("test.mp3", "rw");
raf.seek(startPos);
InputStream is = httpConn.getInputStream();
while ((length = is.read(rtnByte)) != -1) {
raf.write(rtnByte, 0, length);
}
return rtnByte;
}

@Override
public int getContentLength() {
return fileContent.length;
return httpConn.getContentLength();
}

@Override
public void close() {

}

public void setFileContent(byte[] fileContent) {
this.fileContent = fileContent;
httpConn.disconnect();
}

}
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
package com.coderising.download.impl;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.coderising.download.api.Connection;
import com.coderising.download.api.ConnectionException;
import com.coderising.download.api.ConnectionManager;

public class ConnectionManagerImpl implements ConnectionManager {


@Override
public Connection open(String urlStr) throws ConnectionException {
URL url;
try {
url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}

return null;
Connection conn = new ConnectionImpl(urlStr);
return conn;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.coderising.download.utils;

public class FileDownloadUtil {

public static int[] generateDownloadPosArr(int length){
int[] posArr = new int[3];
int firstPos = length/3;
int secondPos = length/3 * 2;

posArr[0] = 0;
posArr[1] = firstPos;
posArr[2] = secondPos;

return posArr;
}
public static void main(String[] args) {
FileDownloadUtil.generateDownloadPosArr(1000);
}

}

0 comments on commit b8c763d

Please sign in to comment.