Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions group09/790466157/src/com/coderising/download/DownloadThread.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
package com.coderising.download;


import com.coderising.download.api.Connection;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;


//import org.omg.CORBA.portable.InputStream;

import com.coderising.download.api.Connection;

public class DownloadThread extends Thread{

Connection conn;
int startPos;
int endPos;
CyclicBarrier barrier ;
String filePath;
String fileName;
CyclicBarrier barrier;

public DownloadThread(CyclicBarrier barrier , Connection conn, int startPos, int endPos , String filePath){

this.barrier = barrier;
public DownloadThread(String name, Connection conn, int startPos, int endPos, String fileName, CyclicBarrier barrier){
super(name);
this.conn = conn;
this.startPos = startPos;
this.endPos = endPos;
this.filePath = filePath;
}
public void run(){
try{
System.out.println("begin download startPos="+startPos+",endPos="+endPos);
byte[] buffer = conn.read(startPos , endPos);
RandomAccessFile file = new RandomAccessFile(filePath, "rw");
file.seek(startPos);
file.write(buffer, 0, buffer.length);
file.close();
barrier.await();
}catch(Exception e){
System.out.println("download error:startPos="+startPos+",endPos="+endPos);
}
this.fileName = fileName;
this.barrier = barrier;
}

public void run(){
try (RandomAccessFile raf = new RandomAccessFile(new File(fileName), "rwd")) {
raf.seek(startPos);
byte[] buf = conn.read(startPos, endPos);
// String desc = Thread.currentThread().getName()+"startPos:"+startPos+",length:"+length + "buf size:"+buf.length;
// System.out.println(desc);
raf.write(buf, 0, buf.length);
if (null != barrier) {
barrier.await();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
} finally {
conn.close();
}
}
}
163 changes: 85 additions & 78 deletions group09/790466157/src/com/coderising/download/FileDownloader.java
Original file line number Diff line number Diff line change
@@ -1,104 +1,111 @@
package com.coderising.download;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.CyclicBarrier;

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 java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class FileDownloader {

String url;

DownloadListener listener;
private String url;
private String fileName;
private DownloadListener listener;
private ConnectionManager cm;
private int threadNum = 5;
private int length = 0;
private Connection conn;

ConnectionManager cm;

private static final int THREAD_NUM = 3;

private static final String BASE_PATH = "C:/Users/Liner/Desktop/Document/Doc";

boolean isFinished = false;

public FileDownloader(String _url) {
public FileDownloader(String _url, String _fileName) {
this.url = _url;
File baseFile = new File(BASE_PATH);
if(!baseFile.exists()){
baseFile.mkdirs();
}
this.fileName = _fileName;

}

public void execute(){

CyclicBarrier barrier = new CyclicBarrier(THREAD_NUM, new Runnable() {

@Override
public void run() {
listener.notifyFinished();
}
});

Connection conn = null;
try {

conn = cm.open(this.url);
// 在这里实现你的代码, 注意: 需要用多线程实现下载
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码
// (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方法

int length = conn.getContentLength();
String filePath = BASE_PATH + "download."+getFileType(this.url);
//System.out.println(filePath);

File file = new File(filePath);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try{
FileOutputStream fos = new FileOutputStream(file);
fos.write(new byte[length], 0, length);//占位
fos.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
try (RandomAccessFile raf = new RandomAccessFile(new File(fileName), "rwd")) {
conn = cm.open(this.url);
length = conn.getContentLength();
raf.setLength(length);
threadPoolDownload();
// oneThreadDownload();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ConnectionException e) {
e.printStackTrace();
}

int blockSize = (length % THREAD_NUM == 0 ) ? length / THREAD_NUM : (length / THREAD_NUM + 1);
for (int i = 0; i < THREAD_NUM; i++) {
int startPos = i * blockSize;
int endPos = startPos + blockSize - 1;
if(endPos >= length - 1){
endPos = length - 1;
}
new DownloadThread(barrier , conn, startPos, endPos , filePath).start();
}
}

} catch (ConnectionException e) {
System.out.println(e.getMessage());
} finally{
if(conn != null){
conn.close();
}
}
public void oneThreadDownload() {
final CyclicBarrier barrier = new CyclicBarrier(1 ,new Runnable() {
@Override
public void run() {
getListener().notifyFinished();
}
});
try {
Thread thread = new DownloadThread("oneThread", conn,0,length, fileName, barrier);
thread.start();
} finally {
if (conn != null) {
conn.close();
}
}
}

}

private String getFileType(String url) {
int index = url.lastIndexOf(".");
return url.substring(index + 1 , url.length());
}
public void threadPoolDownload() throws ConnectionException {
final CyclicBarrier barrier = new CyclicBarrier(threadNum ,new Runnable() {
@Override
public void run() {
getListener().notifyFinished(); // 栅栏
}
});
ExecutorService threadPool = Executors.newCachedThreadPool();
int len = conn.getContentLength();
for(int i = 0; i< threadNum; i++)
{
int start=i*len/ threadNum;
int end = (i+1)*len/ threadNum -1;
conn = cm.open(this.url);
if(i== threadNum -1)
{
end =len;
}
Thread thread = new DownloadThread("thread"+i, conn, start, end, fileName, barrier);
threadPool.execute(thread);
}
if (conn != null) {
conn.close();
}
}

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



public void setConnectionManager(ConnectionManager ucm){
this.cm = ucm;
Expand All @@ -108,4 +115,4 @@ public DownloadListener getListener(){
return this.listener;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

public class FileDownloaderTest {
boolean downloadFinished = false;
private double time = 0;
@Before
public void setUp() throws Exception {
}
Expand All @@ -21,39 +22,34 @@ public void tearDown() throws Exception {
@Test
public void testDownload() {

String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1488796402240&di=8ca9322617d5338cad61232a06f6ed7a&imgtype=0&src=http%3A%2F%2Fjiangsu.china.com.cn%2Fuploadfile%2F2017%2F0212%2F1486868426284307.jpg";

FileDownloader downloader = new FileDownloader(url);
String url = "http://inews.gtimg.com/newsapp_bt/0/1209438116/1000";
// String url = "https://www.baidu.com/img/bd_logo.png";

FileDownloader downloader = new FileDownloader(url, "test.png");

ConnectionManager cm = new ConnectionManagerImpl();
downloader.setConnectionManager(cm);

downloader.setListener(new DownloadListener() {
@Override
public void notifyFinished() {
downloadFinished = true;
}

});


downloader.execute();

// �ȴ����߳����س���ִ�����
// 等待多线程下载程序执行完毕
while (!downloadFinished) {
try {
System.out.println("��û��������ɣ���������");
//����5��
Thread.sleep(5000);
System.out.println("还没有下载完成,休眠0.01秒");
time += 0.01;
//休眠0.01秒
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("������ɣ�");


System.out.println("下载完成!耗时"+time+"秒");

}

}
}
20 changes: 9 additions & 11 deletions group09/790466157/src/com/coderising/download/api/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@

public interface Connection {
/**
* ������ʼ�ͽ���λ�ã� ��ȡ���ݣ� ����ֵ���ֽ�����
* @param startPos ��ʼλ�ã� ��0��ʼ
* @param endPos �����
* 给定开始和结束位置, 读取数据, 返回值是字节数组
* @param startPos 开始位置, 从0开始
* @param endPos 结束位置
* @return
*/
public byte[] read(int startPos,int endPos) throws IOException;
byte[] read(int startPos, int endPos) throws IOException;

/**
* �õ��������ݵij���
* 得到数据内容的长度
* @return
*/
public int getContentLength();
int getContentLength();

/**
* �ر�����
* 关闭连接
*/
public void close();
public Connection open(Object url);
void close();
}


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

public class ConnectionException extends Exception {
}

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

import java.io.IOException;
import java.net.ProtocolException;

public interface ConnectionManager {
/**
* ����һ��url , ��һ������
* 给定一个url , 打开一个连接
* @param url
* @return
*/
public Connection open(String url) throws ConnectionException;
}
Connection open(String url) throws ConnectionException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.coderising.download.api;

public interface DownloadListener {
public void notifyFinished();
}
void notifyFinished();
}
Loading