diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/DownloadThread.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/DownloadThread.java index 8ab66288b0..2c39f00aec 100644 --- a/group14/1091149131/2017JavaPro/src/com/m0312/download/DownloadThread.java +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/DownloadThread.java @@ -42,6 +42,7 @@ public void run(){ os.write(bytes, startPos, endPos-startPos+1); cyclicBarrier.await();//等待其他线程 */ + System.out.println("开始读["+startPos+","+endPos+"]"); byte[] buffer = conn.read(startPos , endPos); RandomAccessFile file = new RandomAccessFile(descFilePath, "rw"); file.seek(startPos); @@ -56,7 +57,7 @@ public void run(){ } catch (BrokenBarrierException e) { e.printStackTrace(); } - System.out.println("所有线程都下载完成"); + //System.out.println("所有线程都下载完成"); //通知 FileDownloader ,自己已经做完 } diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/FileDownloader.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/FileDownloader.java index b6d9102f96..d900a910b6 100644 --- a/group14/1091149131/2017JavaPro/src/com/m0312/download/FileDownloader.java +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/FileDownloader.java @@ -33,7 +33,16 @@ public FileDownloader(String _url) { this.url = _url; } - + private void createPlaceHolderFile(String fileName, int contentLen) throws IOException{ + + RandomAccessFile file = new RandomAccessFile(fileName,"rw"); + + for(int i=0; i totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + return baos.toByteArray(); + */ + /** + * 开始读[0,1603] + 开始读[1604,3207] + is read length: 1024 + is read length: 1024 + baos.size: 1024 + baos.size: 1024 + 开始读[3208,4811] + is read length: 580 + baos.size: 1604 ///size会累积,等于度过的所有buffer size + is read length: 1024 + baos.size: 1024 + is read length: 580 + baos.size: 1604 + is read length: 580 + baos.size: 1604 + */ } @Override diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/impl/ConnectionManagerImpl.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/impl/ConnectionManagerImpl.java index 142b40f2ad..749ec78ca0 100644 --- a/group14/1091149131/2017JavaPro/src/com/m0312/download/impl/ConnectionManagerImpl.java +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/impl/ConnectionManagerImpl.java @@ -12,7 +12,7 @@ public class ConnectionManagerImpl implements ConnectionManager { @Override public Connection open(String url) throws ConnectionException { - Connection con=new ConnectionImpl(); + Connection con=new ConnectionImpl(url); try { URL website = new URL(url); diff --git a/group14/1091149131/2017JavaPro/src/com/test/TestDemo.java b/group14/1091149131/2017JavaPro/src/com/test/TestDemo.java new file mode 100644 index 0000000000..6bc901bc88 --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/test/TestDemo.java @@ -0,0 +1,26 @@ +package com.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class TestDemo { + + @Before + public void setUp() throws Exception { + } + + @Test + public void test() { + String str="123bbb45ddd5ccc567ddd012"; + String[] data=str.split("[0-9]+"); + System.out.println("共拆分"+data.length); + for (String s : data) { + System.out.println(s); + } + System.out.println("===="); + fail("Not yet implemented"); + } + +} diff --git a/group14/1091149131/2017JavaPro/src/com/test/TestThread.java b/group14/1091149131/2017JavaPro/src/com/test/TestThread.java new file mode 100644 index 0000000000..543d52fca0 --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/test/TestThread.java @@ -0,0 +1,37 @@ +package com.test; + +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; +/** + * 所有线程都写完后,才能进行下面任务 + * @author Joy + */ +public class TestThread { + public static void main(String[] args) { + int N = 3; + CyclicBarrier barrier = new CyclicBarrier(N); + for(int i=0;i" + end); + this.start = start; + this.end = end; + this.is = is; + this.raf = raf; + } + + public void run() { + try { + is.skip(start); + raf.seek(start); + // 定义读取输入流内容的的缓存数组(竹筒) + byte[] buff = new byte[BUFF_LEN]; + // 本线程负责下载资源的大小 + long contentLen = end - start; + // 定义最多需要读取几次就可以完成本线程的下载 + long times = contentLen / BUFF_LEN + 4; + // 实际读取的字节数 + int hasRead = 0; + for (int i = 0; i < times; i++) { + hasRead = is.read(buff); + // 如果读取的字节数小于0,则退出循环! + if (hasRead < 0) { + break; + } + raf.write(buff, 0, hasRead); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + // 使用finally块来关闭当前线程的输入流、输出流 + finally { + try { + if (is != null) { + is.close(); + } + if (raf != null) { + raf.close(); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } +} diff --git a/group14/1091149131/2017JavaPro/src/com/test/downfile/MutilDown.java b/group14/1091149131/2017JavaPro/src/com/test/downfile/MutilDown.java new file mode 100644 index 0000000000..21aa754b8b --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/test/downfile/MutilDown.java @@ -0,0 +1,73 @@ +package com.test.downfile; + +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.URL; +import java.net.URLConnection; + +public class MutilDown { + + public static void main(String[] args) { + //定义几个线程去下载 + final int DOWN_THREAD_NUM = 4; + final String OUT_FILE_NAME = "e:/testfile/down.png"; + InputStream[] isArr = new InputStream[DOWN_THREAD_NUM]; + RandomAccessFile[] outArr = new RandomAccessFile[DOWN_THREAD_NUM]; + try { + // 创建一个URL对象 + URL url = new URL("http://127.0.0.3:8082/applogo.png"); + //URL url = new URL("http://hiphotos.baidu.com/240728057/pic/item/6a50e38242aad8f60cf4d2b3.jpg"); + // 以此URL对象打开第一个输入流 + isArr[0] = url.openStream(); + long fileLen = getFileLength(url); + System.out.println("网络资源的大小" + fileLen); + // 以输出文件名创建第一个RandomAccessFile输出流 + //创建从中读取和向其中写入(可选)的随机存取文件流,第一个参数:文件名,第二个参数是:参数指定用以打开文件的访问模式 + //"rw"可能是可读可写, + outArr[0] = new RandomAccessFile(OUT_FILE_NAME, "rw"); + // 创建一个与下载资源相同大小的空文件 + for (int i = 0; i < fileLen; i++) { + outArr[0].write(0); + } + // 每线程应该下载的字节数 + long numPerThred = fileLen / DOWN_THREAD_NUM; + // 整个下载资源整除后剩下的余数取模 + long left = fileLen % DOWN_THREAD_NUM; + for (int i = 0; i < DOWN_THREAD_NUM; i++) { + // 为每个线程打开一个输入流、一个RandomAccessFile对象, + // 让每个线程分别负责下载资源的不同部分。 + //isArr[0]和outArr[0]已经使用,从不为0开始 + if (i != 0) { + // 以URL打开多个输入流 + isArr[i] = url.openStream(); + // 以指定输出文件创建多个RandomAccessFile对象 + outArr[i] = new RandomAccessFile(OUT_FILE_NAME, "rw"); + } + // 分别启动多个线程来下载网络资源 + if (i == DOWN_THREAD_NUM - 1) { + // 最后一个线程下载指定numPerThred+left个字节 + new DownThread(i * numPerThred, (i + 1) * numPerThred + + left, isArr[i], outArr[i]).start(); + } else { + // 每个线程负责下载一定的numPerThred个字节 + new DownThread(i * numPerThred, (i + 1) * numPerThred, + isArr[i], outArr[i]).start(); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + // 定义获取指定网络资源的长度的方法 + public static long getFileLength(URL url) throws Exception { + long length = 0; + // 打开该URL对应的URLConnection + URLConnection con = url.openConnection(); + // 获取连接URL资源的长度 + long size = con.getContentLength(); + length = size; + return length; + } + +}