Skip to content

Commit

Permalink
Merge pull request #26 from northSmall/master
Browse files Browse the repository at this point in the history
pr-提交多线程下载作业
  • Loading branch information
zeyuanpinghe authored Mar 16, 2017
2 parents fac911c + 9c0f020 commit e4e110a
Show file tree
Hide file tree
Showing 17 changed files with 397 additions and 7 deletions.
33 changes: 33 additions & 0 deletions group19/604322962/learning2017/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.north</groupId>
<artifactId>learning2017</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main.java.com.coding.basic;
package com.coding.basic;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main.java.com.coding.basic;
package com.coding.basic;

public class BinaryTreeNode {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main.java.com.coding.basic;
package com.coding.basic;

public interface Iterator {
public boolean hasNext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main.java.com.coding.basic;
package com.coding.basic;

import java.util.NoSuchElementException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main.java.com.coding.basic;
package com.coding.basic;

public interface List {
public void add(Object o);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main.java.com.coding.basic;
package com.coding.basic;

import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main.java.com.coding.basic;
package com.coding.basic;

import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.coding.download;

import com.coding.download.api.Connection;
import com.coding.download.api.ConnectionException;
import com.coding.download.impl.ConnectionManagerImpl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.CountDownLatch;

public class DownloadThread extends Thread{

Connection conn;
int startPos;
int endPos;
String filelocal;
CountDownLatch cdl;
public DownloadThread(Connection conn, int startPos, int endPos, String filelocal, CountDownLatch cdl){

this.conn = conn;
this.startPos = startPos;
this.endPos = endPos;
this.filelocal = filelocal;
this.cdl = cdl;
}
public void run(){
/*try {
RandomAccessFile fos = new RandomAccessFile("C:\\Users\\gaokun\\Desktop\\test3.jpg", "rwd");
byte[] read = conn.read(startPos, endPos);
fos.seek(startPos);
fos.setLength(endPos-startPos);
fos.write(read);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}*/
RandomAccessFile raf = null;
try {
String url = "http://www.dabaoku.com/sucaidatu/dongwu/chongwujingling/804838.JPG";
byte[] buf = conn.read(startPos, endPos);
raf = new RandomAccessFile(filelocal, "rwd");
raf.seek(startPos);
raf.write(buf);
raf.close();
cdl.countDown();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.coding.download;

import com.coding.download.api.Connection;
import com.coding.download.api.ConnectionException;
import com.coding.download.api.ConnectionManager;
import com.coding.download.api.DownloadListener;

import java.io.File;
import java.util.concurrent.CountDownLatch;


public class FileDownloader {

String url;

DownloadListener listener;

ConnectionManager cm;


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

}

public void execute() throws InterruptedException, ConnectionException {
// 在这里实现你的代码, 注意: 需要用多线程实现下载
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码
// (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;
try {
conn = cm.open(this.url);
int length = conn.getContentLength();
new DownloadThread(conn,0,length-1).start();
} catch (ConnectionException e) {
e.printStackTrace();
}finally{
if(conn != null){
conn.close();
}
}*/
//Connection conn = null;
/*try {
int threadCount = 3;
Connection conn = cm.open(this.url);
int length = conn.getContentLength();
int blockSize = length / threadCount;
for (int threadId = 1; threadId <= threadCount; threadId++) {
//第一个线程下载的开始位置
int startIndex = (threadId - 1) * blockSize;
int endIndex = threadId * blockSize - 1;
if (threadId == threadCount) {//最后一个线程下载的长度要稍微长一点
endIndex = length;
}
System.out.println("线程:"+threadId+"下载:---"+startIndex+"--->"+endIndex);
new DownloadThread(conn, startIndex, endIndex).start();
}
} catch (ConnectionException e) {
e.printStackTrace();
}*/

int threadCount = 3;
int startPos;
int endPos;
String filelocaltion = "C:\\Users\\gaokun\\Desktop\\demo.jpg";
CountDownLatch cdl = new CountDownLatch(3);
for (int i = 0; i < threadCount; i++) {
Connection conn = cm.open(url);
int fileLength = conn.getContentLength();
startPos = i*fileLength/threadCount;
endPos = (i+1)*fileLength/threadCount-1;
if (i == threadCount-1)
endPos = fileLength-1;
new DownloadThread(conn, startPos, endPos, filelocaltion, cdl).start();
}
cdl.await();
System.out.println("线程跑完了!");
listener.notifyFinished();

}

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



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

public DownloadListener getListener(){
return this.listener;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.coding.download;

import com.coding.download.api.ConnectionException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.coding.download.api.ConnectionManager;
import com.coding.download.api.DownloadListener;
import com.coding.download.impl.ConnectionManagerImpl;

public class FileDownloaderTest {
boolean downloadFinished = false;
@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testDownload() throws ConnectionException, InterruptedException, ConnectionException {

String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1489601005973&di=e104648a3c8dcaabb18dfb5d98870d84&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fbaike%2Fpic%2Fitem%2F3b292df5e0fe992536be579530a85edf8cb17140.jpg";

FileDownloader downloader = new FileDownloader(url);

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);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("下载完成!");



}

void testRead(){

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.coding.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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.coding.download.api;

public class ConnectionException extends Exception {

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

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

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

0 comments on commit e4e110a

Please sign in to comment.