Skip to content

Commit

Permalink
Merge pull request #39 from dongyuanlongwang/master
Browse files Browse the repository at this point in the history
第三周的作业终于完成了
  • Loading branch information
nusubmarine01 authored Mar 18, 2017
2 parents 74c742b + fe21787 commit fae63e5
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,35 @@

import rui.study.coding2017.jobs3.download.api.Connection;

import java.io.IOException;
import java.io.RandomAccessFile;

import static rui.study.coding2017.jobs3.download.FileDownloader.getFilePath;

public class DownloadThread extends Thread{

Connection conn;
int startPos;
int endPos;

public DownloadThread(Connection conn, int startPos, int endPos){

this.conn = conn;
this.conn = conn;
this.startPos = startPos;
this.endPos = endPos;
}
public void run(){

@Override
public void run(){
try {
byte[] bytes=conn.read(startPos,endPos);
RandomAccessFile randomAccessFile=new RandomAccessFile(getFilePath(),"rw");
randomAccessFile.seek(startPos);
randomAccessFile.write(bytes);
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
conn.close();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,121 @@
import rui.study.coding2017.jobs3.download.api.ConnectionManager;
import rui.study.coding2017.jobs3.download.api.DownloadListener;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

public class FileDownloader {

public static int threadNum=5;

String url;

DownloadListener listener;

ConnectionManager cm;


private static String filePath;

static String path="D:\\360downloads";

File pathFile;

public static String getFilePath() {
return filePath;
}

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

filePath=path+"/"+url.substring(url.lastIndexOf("/")+1);

pathFile=new File(path);
if(!pathFile.exists()||!pathFile.isDirectory()){
pathFile.mkdir();
}
}


public void setListener(DownloadListener listener) {
this.listener = listener;
}

public void setConnectionManager(ConnectionManager ucm){
this.cm = ucm;
}

public void execute(){
// 在这里实现你的代码, 注意: 需要用多线程实现下载
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码
// (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定)
// (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有
// 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。
// 具体的实现思路:
// 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度
// 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法
// 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组
// 3. 把byte数组写入到文件中
// 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法

// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
Connection conn = null;
Connection conn = null;
try {

conn = cm.open(this.url);

int length = conn.getContentLength();
new DownloadThread(conn,0,length-1).start();
int length = conn.getContentLength();
setRandomAccessFile(length);
List<DownloadThread> list = getDownloadThreads(conn, length);
waitForDownLoad(list);
listener.notifyFinished();
} catch (ConnectionException e) {
e.printStackTrace();
}finally{
} catch (IOException 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;
}

/**
* 配置 随机访问文件
* 预分配文件所占的磁盘空间,磁盘中会创建一个指定大小的文件;
* @param length
* @throws IOException
*/
private void setRandomAccessFile(int length) throws IOException {
try {
RandomAccessFile randomAccessFile=new RandomAccessFile(filePath,"rw");
randomAccessFile.setLength(length);
randomAccessFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

/**
* 获得下载线程,为下一步阻塞做准备
* @param conn 连接
* @param length 长度
* @return
*/
private List<DownloadThread> getDownloadThreads(Connection conn, int length) {
int startPos;
int endPos;
List<DownloadThread> list=new ArrayList<DownloadThread>();
for (int i = 0; i < threadNum; i++) {
//从0开始
startPos=i*(length/threadNum);
//从length/threadNum开始,最后为所有长度
endPos=(i==threadNum-1)?length-1:(i+1)*(length/threadNum);

DownloadThread downloadThread=new DownloadThread(conn,startPos,endPos);
downloadThread.start();
list.add(downloadThread);
}
return list;
}

/**
* 阻塞线程列表,等待线程列表全部执行完成
* @param list 线程列表
*/
private void waitForDownLoad(List<DownloadThread> list) {
for (DownloadThread aList : list) {
try {
aList.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package rui.study.coding2017.jobs3.download.api;

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

public interface ConnectionManager {
/**
* 给定一个url , 打开一个连接
* @param url
* @return
*/
public Connection open(String url) throws ConnectionException;
public Connection open(String url) throws ConnectionException, IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,55 @@

import rui.study.coding2017.jobs3.download.api.Connection;

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

public class ConnectionImpl implements Connection {
private static int byteSize = 8 * 1024;

private int contentLength;


private URL url;

public ConnectionImpl(URL url) throws IOException {
this.url = url;
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
contentLength = httpURLConnection.getContentLength();
httpURLConnection.disconnect();
}

@Override
public byte[] read(int startPos, int endPos) throws IOException {
BufferedInputStream bufferedInputStream;

ByteArrayOutputStream outputStream;

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos);
bufferedInputStream = new BufferedInputStream(httpURLConnection.getInputStream());
outputStream = new ByteArrayOutputStream();
int temp;
byte[] bytes = new byte[byteSize];
while ((temp = bufferedInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, temp);
}
bufferedInputStream.close();
outputStream.flush();
outputStream.close();
httpURLConnection.disconnect();
return outputStream.toByteArray();
}

@Override
public int getContentLength() {
return contentLength;
}

@Override
public void close() {
}

@Override
public byte[] read(int startPos, int endPos) throws IOException {

return null;
}

@Override
public int getContentLength() {

return 0;
}

@Override
public void close() {


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import rui.study.coding2017.jobs3.download.api.ConnectionException;
import rui.study.coding2017.jobs3.download.api.ConnectionManager;

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

public class ConnectionManagerImpl implements ConnectionManager {

@Override
public Connection open(String url) throws ConnectionException {

return null;
public Connection open(String url) throws ConnectionException, IOException {
return new ConnectionImpl(new URL(url));
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rui.study.coding2017.jobs3;
package rui.study.coding2017.jobs3.download;

import org.junit.After;
import org.junit.Before;
Expand All @@ -22,8 +22,8 @@ public void tearDown() throws Exception {
@Test
public void testDownload() {

String url = "http://localhost:8080/test.jpg";
String url = "http://sw.bos.baidu.com/sw-search-sp/software/952c9d6e73f50/QQ_8.9.20029.0_setup.exe";

FileDownloader downloader = new FileDownloader(url);


Expand All @@ -35,7 +35,6 @@ public void testDownload() {
public void notifyFinished() {
downloadFinished = true;
}

});


Expand All @@ -52,9 +51,6 @@ public void notifyFinished() {
}
}
System.out.println("下载完成!");



}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package rui.study.coding2017.jobs3.download.impl;

import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import static org.junit.Assert.*;

/**
* 测试链接实现
* Created by 赵睿 on 2017/3/15.
*/
public class ConnectionImplTest {
private String url="http://sw.bos.baidu.com/sw-search-sp/software/952c9d6e73f50/QQ_8.9.20029.0_setup.exe";

private ConnectionImpl connection=new ConnectionImpl(new URL(url));

public ConnectionImplTest() throws IOException {
}

@Test
public void read() throws Exception {
byte[] bs=connection.read(0,connection.getContentLength());
System.out.println(bs.length);
FileOutputStream fileOutputStream=new FileOutputStream(new File("D://eee.exe"));
fileOutputStream.write(bs);
fileOutputStream.flush();
fileOutputStream.close();
}

@Test
public void getContentLength() throws Exception {
System.out.println(connection.getContentLength());
}

}

0 comments on commit fae63e5

Please sign in to comment.