forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
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 #22 from yanghaitao0410/master
第三周
- Loading branch information
Showing
14 changed files
with
943 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
50 changes: 50 additions & 0 deletions
50
group06/1454385822/src/com/coding/basic/homework_03/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.coding.basic.homework_03.download; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.RandomAccessFile; | ||
|
||
import com.coding.basic.homework_03.download.api.Connection; | ||
|
||
public class DownloadThread extends Thread{ | ||
|
||
Connection conn; | ||
int startPos; | ||
int endPos; | ||
boolean downOver = false; | ||
|
||
public DownloadThread( Connection conn, int startPos, int endPos){ | ||
|
||
this.conn = conn; | ||
this.startPos = startPos; | ||
this.endPos = endPos; | ||
} | ||
public void run(){ | ||
RandomAccessFile raf = null; | ||
try { | ||
// System.out.println(Thread.currentThread().getName() + "开始下载..."); | ||
byte[] bytes = conn.read(startPos, endPos); | ||
|
||
// if(!file.exists()){ | ||
// file.createNewFile(); | ||
// } | ||
synchronized(this){ | ||
File file = new File("copy.jpg"); | ||
raf = new RandomAccessFile(file,"rw"); | ||
raf.seek(startPos); | ||
raf.write(bytes); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
}finally{ | ||
if(raf != null){ | ||
try { | ||
raf.close(); | ||
downOver = true; | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
group06/1454385822/src/com/coding/basic/homework_03/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,130 @@ | ||
package com.coding.basic.homework_03.download; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
|
||
import com.coding.basic.homework_03.download.api.Connection; | ||
import com.coding.basic.homework_03.download.api.ConnectionManager; | ||
import com.coding.basic.homework_03.download.api.DownloadListener; | ||
|
||
public class FileDownloader { | ||
|
||
String url; | ||
|
||
DownloadListener listener; | ||
|
||
ConnectionManager cm; | ||
private static final long KBSIZE = 1024; | ||
private static final long MBSIZE = 1024 * 1024; | ||
|
||
public FileDownloader(String _url) { | ||
this.url = _url; | ||
|
||
} | ||
|
||
public void execute() throws IOException { | ||
// 在这里实现你的代码, 注意: 需要用多线程实现下载 | ||
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 | ||
// (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(); | ||
createFile(new File("copy.jpg"), length); | ||
// System.out.println(length); | ||
int len = length / 5; | ||
DownloadThread t1 = new DownloadThread(cm.open(this.url), 0, len - 1); | ||
DownloadThread t2 = new DownloadThread(cm.open(this.url), len, len * 2 - 1); | ||
DownloadThread t3 = new DownloadThread(cm.open(this.url), len * 2, len * 3 - 1); | ||
DownloadThread t4 = new DownloadThread(cm.open(this.url), len * 3, len * 4 - 1); | ||
DownloadThread t5 = new DownloadThread(cm.open(this.url), len * 4, len * 5 - 1); | ||
DownloadThread t6 = new DownloadThread(cm.open(this.url), len * 5, length - 1); | ||
t1.start(); | ||
t2.start(); | ||
t3.start(); | ||
t4.start(); | ||
t5.start(); | ||
t6.start(); | ||
|
||
while (!(t1.downOver && t2.downOver && t3.downOver && t4.downOver && t5.downOver && t6.downOver)) { | ||
try { | ||
System.out.println("还有线程没有下载完成,等待五秒..."); | ||
// 休眠5秒 | ||
Thread.sleep(5000); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (conn != null) { | ||
conn.close(); | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 创建一个临时文件 | ||
* | ||
* @param file | ||
* @param length | ||
* @throws IOException | ||
*/ | ||
private void createFile(File file, int length) throws IOException { | ||
long batchSize = 0; | ||
FileOutputStream fos = null; | ||
if (!file.exists()) { | ||
file.createNewFile(); | ||
} | ||
|
||
if (length > KBSIZE) { | ||
batchSize = KBSIZE; | ||
} | ||
if (length > MBSIZE) { | ||
batchSize = MBSIZE; | ||
} | ||
fos = new FileOutputStream(file); | ||
int times = length / (int) batchSize; | ||
int last = length % (int) batchSize; | ||
FileChannel channel = fos.getChannel(); | ||
for (int i = 0; i < times; i++) { | ||
ByteBuffer buffer = ByteBuffer.allocate((int) batchSize); | ||
channel.write(buffer); | ||
} | ||
ByteBuffer buffer = ByteBuffer.allocate(last); | ||
channel.write(buffer); | ||
fos.close(); | ||
|
||
} | ||
|
||
public void setListener(DownloadListener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
public void setConnectionManager(ConnectionManager ucm) { | ||
this.cm = ucm; | ||
} | ||
|
||
public DownloadListener getListener() { | ||
return this.listener; | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
group06/1454385822/src/com/coding/basic/homework_03/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,57 @@ | ||
package com.coding.basic.homework_03.download; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.coding.basic.homework_03.download.api.ConnectionManager; | ||
import com.coding.basic.homework_03.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 IOException { | ||
|
||
String url = "http://bpic.588ku.com/element_origin_min_pic/16/12/01/b4365c64e8a567afd0ab63285515de55.jpg"; | ||
|
||
FileDownloader downloader = new FileDownloader(url); | ||
ConnectionManager cm = new ConnectionManagerImpl(); | ||
downloader.setConnectionManager(cm); | ||
downloader.execute(); | ||
|
||
// downloader.setListener(new DownloadListener() { | ||
// @Override | ||
// public void notifyFinished() { | ||
// downloadFinished = true; | ||
// } | ||
// }); | ||
|
||
|
||
// 等待多线程下载程序执行完毕 | ||
// while (!downloadFinished) { | ||
// try { | ||
//// System.out.println(Thread.currentThread().getName()); | ||
// System.out.println("还没有下载完成,休眠五秒"); | ||
// //休眠5秒 | ||
//// Thread.sleep(1000); | ||
// } catch (Exception e) { | ||
// e.printStackTrace(); | ||
// } | ||
// } | ||
System.out.println("下载完成!文件存放在此项目根目录下"); | ||
|
||
|
||
|
||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
group06/1454385822/src/com/coding/basic/homework_03/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,26 @@ | ||
package com.coding.basic.homework_03.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 | ||
* @throws IOException | ||
*/ | ||
public int getContentLength() throws IOException; | ||
|
||
/** | ||
* 关闭连接 | ||
* @throws IOException | ||
*/ | ||
public void close() throws IOException; | ||
} |
5 changes: 5 additions & 0 deletions
5
group06/1454385822/src/com/coding/basic/homework_03/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.coding.basic.homework_03.download.api; | ||
|
||
public class ConnectionException extends Exception { | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
group06/1454385822/src/com/coding/basic/homework_03/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,16 @@ | ||
package com.coding.basic.homework_03.download.api; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
|
||
public interface ConnectionManager { | ||
/** | ||
* 给定一个url , 打开一个连接 | ||
* @param url | ||
* @return | ||
* @throws FileNotFoundException | ||
* @throws IOException | ||
*/ | ||
public Connection open(String url) throws IOException; | ||
} | ||
|
5 changes: 5 additions & 0 deletions
5
group06/1454385822/src/com/coding/basic/homework_03/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.coding.basic.homework_03.download.api; | ||
|
||
public interface DownloadListener { | ||
public void notifyFinished(); | ||
} |
75 changes: 75 additions & 0 deletions
75
group06/1454385822/src/com/coding/basic/homework_03/download/impl/ConnectionImpl.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,75 @@ | ||
package com.coding.basic.homework_03.download.impl; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
import com.coding.basic.homework_03.download.api.Connection; | ||
|
||
public class ConnectionImpl implements Connection{ | ||
// public RandomAccessFile raf = null; | ||
public HttpURLConnection httpUrl = null; | ||
public URL Url = null; | ||
// public BufferedInputStream bis = null; | ||
// FileInputStream fis = null; | ||
InputStream is = null; | ||
public ConnectionImpl(String url) throws IOException{ | ||
|
||
Url = new URL(url); | ||
httpUrl = (HttpURLConnection) Url.openConnection(); | ||
httpUrl.connect(); | ||
is = httpUrl.getInputStream(); | ||
|
||
// fis = (FileInputStream) httpUrl.getInputStream(); | ||
// bis = new BufferedInputStream(httpUrl.getInputStream()); | ||
// raf = new RandomAccessFile(url, "r"); | ||
} | ||
|
||
@Override | ||
public byte[] read(int startPos, int endPos) throws IOException { | ||
int length = endPos - startPos + 1; | ||
byte[] result = new byte[length]; | ||
int num = 0; | ||
if(startPos != 0){ | ||
is.skip(startPos); | ||
} | ||
num = (int)is.read(result); | ||
// System.out.println("length : " + length); | ||
// System.out.println("num : " + num); | ||
|
||
// System.out.println(startPos +"....读到了...."+endPos); | ||
// | ||
// System.out.println(Integer.MAX_VALUE); | ||
// System.out.println("num:" + num); | ||
// fis.skip(startPos); | ||
// fis.read(result, startPos, length); | ||
// bis.read(result, startPos, length); | ||
|
||
// raf.seek(startPos); | ||
// raf.read(result); | ||
return result; | ||
} | ||
|
||
@Override | ||
public int getContentLength() throws IOException { | ||
int num = 0; | ||
return httpUrl.getContentLength(); | ||
// return (int) raf.length(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
// if(raf != null){ | ||
// raf.close(); | ||
// } | ||
// if(bis != null){ | ||
// bis.close(); | ||
// } | ||
if(is != null){ | ||
is.close(); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.