diff --git a/group27/1252327158/task1_20170312/src/com/coding/ArrayListTest.java b/group27/1252327158/task1_20170312/src/com/coding/ArrayListTest.java index a96d7287fb..4370a34a3d 100644 --- a/group27/1252327158/task1_20170312/src/com/coding/ArrayListTest.java +++ b/group27/1252327158/task1_20170312/src/com/coding/ArrayListTest.java @@ -9,7 +9,7 @@ import org.junit.Test; public class ArrayListTest { - + ArrayList list; @Before diff --git a/group27/1252327158/task1_20170312/src/com/coding/LinkedList.java b/group27/1252327158/task1_20170312/src/com/coding/LinkedList.java index 168bb07592..7226968638 100644 --- a/group27/1252327158/task1_20170312/src/com/coding/LinkedList.java +++ b/group27/1252327158/task1_20170312/src/com/coding/LinkedList.java @@ -183,7 +183,16 @@ public Node(T data, Node node) { * 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ public void reverse(){ - + if (size <= 1) { + return; + } + Node node = head; + while (node.next != null) { + Node temp = node.next; + node.next = temp.next; + temp.next = head; + head = temp; + } } /** @@ -193,7 +202,11 @@ public void reverse(){ */ public void removeFirstHalf(){ - + if (size < 2) { + return; + } + int delSize = (int)Math.floor(size/2); + remove(0, delSize); } /** @@ -202,8 +215,31 @@ public void removeFirstHalf(){ * @param length */ public void remove(int i, int length){ + if (i < 0 || i >= size || length < 0 || i + length > size) { + throw new IndexOutOfBoundsException(); + } + if (i == 0) { + head = removeStartWith(head, length); + return; + } + Node beforeStart = head; //被删除元素的前一个 + for (int index = 1; index < i; index++) { + beforeStart = beforeStart.next; + } + beforeStart.next = removeStartWith(beforeStart.next, length); + } + private Node removeStartWith(Node startNode, int length) { + Node node = null; + for (int index = 1; index <= length; index++) { + node = startNode; + startNode = startNode.next; + node.next = null; + size--; + } + return startNode; } + /** * 假定当前链表和list均包含已升序排列的整数 * 从当前链表中取出那些list所指定的元素 @@ -212,8 +248,29 @@ public void remove(int i, int length){ * 返回的结果应该是[101,301,401,601] * @param list */ - public static int[] getElements(LinkedList list){ - return null; + public int[] getElements(LinkedList list){ + if (size == 0 || list == null || list.size == 0) { + return new int[0]; + } + int[] result = new int[list.size]; + Node node = head; + int index = 0; + int resultIndex = 0; + for (int i = 0; i < size; i++ ) { + int listData = ((Integer)list.get(index)).intValue(); + if ( listData >= size) { + throw new IndexOutOfBoundsException(); + } + if (i == listData) { + result[resultIndex++] = ((Integer)node.data).intValue(); + index++; + } + if (index == list.size || listData == size) { + break; + } + node = node.next; + } + return result; } /** @@ -224,7 +281,35 @@ public static int[] getElements(LinkedList list){ */ public void subtract(LinkedList list){ - + if (list == null || list.size() == 0) { + return; + } + Node node = head; + Node beforeNode = null; + Node temp = null; + int j = 0; //参数list索引 + for (;node != null && j < list.size() ;) { + int paradata = ((Integer)list.get(j)).intValue(); + int data = ((Integer)node.data).intValue(); + if (data == paradata) { + j++; + size--; + temp = node; + if (beforeNode == null) { + head = node.next; + node = node.next; + } else {; + beforeNode.next = node.next; + node = node.next; + } + temp.next = null; + } else if (data < paradata) { + beforeNode = node; + node = node.next; + } else { + j++; + } + } } /** @@ -232,7 +317,21 @@ public void subtract(LinkedList list){ * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) */ public void removeDuplicateValues(){ - + if (size < 2) { + return; + } + Node node = head; + Node delNode = null; + while (node.next != null) { + if (((Integer)node.next.data).equals(node.data)) { + delNode = node.next; + node.next = node.next.next; + delNode.next = null; + size--; + } else { + node = node.next; + } + } } /** @@ -242,7 +341,27 @@ public void removeDuplicateValues(){ * @param max */ public void removeRange(int min, int max){ - + if (min >= max) { + return; + } + Node node = head; + int delLen = 0; + int startIndex = -1; + for (int i = 0; i < size; i++) { + int currentData = ((Integer)node.data).intValue(); + if (currentData > min && currentData < max) { + if (delLen == 0) { + startIndex = i; + } + delLen++; + } else if (currentData >= max) { + break; + } + node = node.next; + } + if (delLen > 0) { + remove(startIndex, delLen); + } } /** @@ -251,6 +370,24 @@ public void removeRange(int min, int max){ * @param list */ public LinkedList intersection( LinkedList list){ - return null; + if (list.size() == 0 || size == 0) { + return null; + } + LinkedList result = new LinkedList(); + Node node = head; + Iterator listIter = list.iterator(); + while (listIter.hasNext()) { + int listData = ((Integer)listIter.next()).intValue(); + for (;node != null;) { + int currentData = ((Integer)node.data).intValue(); + if (currentData == listData) { + result.addLast(currentData); + } else if (currentData > listData) { + break; + } + node = node.next; + } + } + return result; } } diff --git a/group27/1252327158/task1_20170312/src/com/coding/LinkedListTest.java b/group27/1252327158/task1_20170312/src/com/coding/LinkedListTest.java index 03f7198998..c9a2504964 100644 --- a/group27/1252327158/task1_20170312/src/com/coding/LinkedListTest.java +++ b/group27/1252327158/task1_20170312/src/com/coding/LinkedListTest.java @@ -8,7 +8,8 @@ import org.junit.Test; public class LinkedListTest { - LinkedList list; + + LinkedList list; @Before public void setUp() throws Exception { @@ -92,4 +93,118 @@ public void testIterator() { } } + @Test + public void reverse() throws Exception { + list.add("third"); + list.add("forth"); + Assert.assertEquals("forth", list.get(0)); + list.reverse(); + Assert.assertEquals("first", list.get(0)); + Assert.assertEquals("second", list.get(1)); + Assert.assertEquals("third", list.get(2)); + Assert.assertEquals("forth", list.get(3)); + } + + @Test + public void removeFirstHalf() throws Exception { + list.add("third"); + list.add("forth"); + list.removeFirstHalf(); + Assert.assertEquals("second", list.get(0)); + Assert.assertEquals(2, list.size()); + } + + @Test + public void remove() throws Exception { + list.add("third"); + list.add("forth"); + list.remove(1, 2); + Assert.assertEquals("forth", list.get(0)); + Assert.assertEquals("first", list.get(1)); + } + + @Test + public void getElements() throws Exception { + LinkedList intList = new LinkedList<>(); + intList.addLast(11); + intList.addLast(101); + intList.addLast(201); + intList.addLast(301); + intList.addLast(401); + intList.addLast(501); + intList.addLast(601); + intList.addLast(701); + LinkedList searchList = new LinkedList<>(); + searchList.addLast(1); + searchList.addLast(3); + searchList.addLast(4); + searchList.addLast(7); + + Assert.assertArrayEquals(new int[]{101,301,401,701}, intList.getElements(searchList)); + } + + @Test + public void subtract() throws Exception { + LinkedList intList = new LinkedList<>(); + intList.addLast(11); + intList.addLast(101); + intList.addLast(201); + intList.addLast(301); + intList.addLast(401); + LinkedList delList= new LinkedList<>(); + delList.addLast(11); + delList.addLast(101); + delList.addLast(301); + delList.addLast(401); + intList.subtract(delList); + Assert.assertEquals(201, ((Integer)intList.get(0)).intValue()); + Assert.assertEquals(1, intList.size()); + } + + @Test + public void removeDuplicateValues() throws Exception { + LinkedList intList = new LinkedList<>(); + intList.addLast(11); + intList.addLast(101); + intList.addLast(101); + intList.addLast(101); + intList.addLast(401); + intList.removeDuplicateValues(); + Assert.assertEquals(11, ((Integer)intList.get(0)).intValue()); + Assert.assertEquals(101, ((Integer)intList.get(1)).intValue()); + Assert.assertEquals(401, ((Integer)intList.get(2)).intValue()); + Assert.assertEquals(3, intList.size()); + } + + @Test + public void removeRange() throws Exception { + LinkedList intList = new LinkedList<>(); + intList.addLast(11); + intList.addLast(101); + intList.addLast(201); + intList.addLast(301); + intList.addLast(401); + intList.removeRange(11, 301); + Assert.assertEquals(3, intList.size()); + Assert.assertEquals(11, ((Integer)intList.get(0)).intValue()); + Assert.assertEquals(301, ((Integer)intList.get(1)).intValue()); + } + + @Test + public void intersection() throws Exception { + LinkedList intList = new LinkedList<>(); + intList.addLast(11); + intList.addLast(101); + intList.addLast(201); + intList.addLast(301); + intList.addLast(401); + LinkedList paraList= new LinkedList<>(); + paraList.addLast(11); + paraList.addLast(301); + paraList.addLast(501); + LinkedList newList = intList.intersection(paraList); + Assert.assertEquals(2, newList.size()); + Assert.assertEquals(11, ((Integer)newList.get(0)).intValue()); + Assert.assertEquals(301, ((Integer)newList.get(1)).intValue()); + } } diff --git a/group27/1252327158/task2_20170319/litestruts/src/com/coderising/litestruts/Struts.java b/group27/1252327158/task2_20170319/litestruts/src/com/coderising/litestruts/Struts.java index ce0ec1a04c..3d5ed76bb3 100644 --- a/group27/1252327158/task2_20170319/litestruts/src/com/coderising/litestruts/Struts.java +++ b/group27/1252327158/task2_20170319/litestruts/src/com/coderising/litestruts/Struts.java @@ -39,17 +39,28 @@ public static View runAction(String actionName, Map parameters) { */ - XMLHandle xmlHandle; + Document document = null; Class action = null; Object object = null; - + try { + SAXReader saxReader = new SAXReader(); + document = saxReader.read(new File("struts.xml")); // 读取XML文件,获得document对象 + Element root = document.getRootElement(); + for (Iterator it = root.elementIterator(); it.hasNext();) { + Element element = it.next(); + if (element.attribute("name").getValue().equals(actionName)) { + String className = element.attribute("class").getValue(); + action = Class.forName(className); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + if (action == null) { + return null; + } + try { - xmlHandle = new XMLHandle(); - String className = xmlHandle.getClassName(actionName); - if (className == null) { - return null; - } - action = Class.forName(className); object = action.newInstance(); Iterator> mapIt = parameters.entrySet().iterator(); while (mapIt.hasNext()) { @@ -58,28 +69,35 @@ public static View runAction(String actionName, Map parameters) { for (Method method : methods) { if (method.getName().equalsIgnoreCase("set" + entry.getKey())) { method.invoke(object, entry.getValue()); + break; } - } + } +// methods = action.getDeclaredMethods(); +// for (Method method : methods) { +// if (method.getName().equalsIgnoreCase("get" + entry.getKey())) { +// String name = (String)method.invoke(object); +// System.out.println(name); +// break; +// } +// } } - + } + } + Method execute = action.getDeclaredMethod("execute"); String result = (String)execute.invoke(object); - Field[] fields = action.getDeclaredFields(); + Method[] methods = action.getMethods(); Map map = new HashMap(); - for (Field field : fields) { - Method[] methods = action.getMethods(); - for (Method method : methods) { - if (method.getName().equalsIgnoreCase("get" + field.getName())) { - map.put(field.getName(), (String)method.invoke(object)); - } + for (Method method : methods) { + if (method.getName().startsWith("get")) { + map.put(method.getName().substring(3).toLowerCase(), (String)method.invoke(object)); } } + View view = new View(); - view.setParameters(map); - String jspResult = xmlHandle.getResult(actionName, result); - view.setJsp(jspResult); - return view; + view.setParameters(map); + return view; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { @@ -92,10 +110,6 @@ public static View runAction(String actionName, Map parameters) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); - } catch (DocumentException e) { - e.printStackTrace(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); } diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/DownloadThread.java b/group27/1252327158/task3_20170326/download/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..58b8a4e3a6 --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/DownloadThread.java @@ -0,0 +1,79 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +import java.io.IOException; +import java.io.RandomAccessFile; + +public class DownloadThread extends Thread{ + + private int endPos; + private int startPos; + private String url; + private String destFilePath; + private ConnectionManager connManager; + private DownloadListener downloadListener; + + public DownloadThread(ConnectionManager connManager, String url, int startPos, int endPos, String destFilePath, + DownloadListener downloadListener) { + + this.url = url; + this.endPos = endPos; + this.startPos = startPos; + this.connManager = connManager; + this.destFilePath = destFilePath; + this.downloadListener = downloadListener; + } + + @Override + public void run() { + Connection conn = null; + RandomAccessFile randomAccessFile = null; + try { + doLog("BIN"); + conn = connManager.open(url, startPos, endPos); + byte[] read = conn.read(startPos, endPos); + String _filePath = destFilePath; + if (_filePath == null || _filePath.length() == 0) { + _filePath = conn.getFileName(); + } + randomAccessFile = new RandomAccessFile(_filePath, "rw"); + randomAccessFile.seek(startPos); + randomAccessFile.write(read); + doLog("END"); + } catch (IOException e) { + doLog("EXP1"); + e.printStackTrace(); + } catch (com.coderising.download.api.ConnectionException e) { + doLog("EXP2"); + e.printStackTrace(); + } finally { + if (randomAccessFile != null) { + try { + randomAccessFile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (conn != null) { + conn.close(); + } + if (downloadListener != null) { + downloadListener.notifyFinished(); + } + } + } + + private void doLog(String action) { + System.out.println( + "*********** " + action + + " [" + + startPos + + "-" + + endPos + + "]" + + " ***********"); + } +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/FileDownloader.java b/group27/1252327158/task3_20170326/download/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..ee3a321b81 --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/FileDownloader.java @@ -0,0 +1,83 @@ +package com.coderising.download; + +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +import java.util.concurrent.atomic.AtomicInteger; + + +public class FileDownloader { + + private String url; + + private DownloadListener listener; + + private ConnectionManager cm; + + private AtomicInteger atomicInteger; + + public FileDownloader(String _url) { + this.url = _url; + atomicInteger = new AtomicInteger(); + } + + /** + * 在这里实现你的代码, 注意: 需要用多线程实现下载 + * 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + * (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方法 + * + * 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + */ + public void execute() { + try { + + int threadCount = 5; + int length = this.cm.getContentLength(this.url); + for (int i = 0; i < threadCount; i++) { + + int threadLoadLength = length / threadCount; + int startPos = threadLoadLength * i; + int endPos; + if (i != threadCount - 1) { + endPos = threadLoadLength * (i + 1) - 1; + } else { + endPos = length - 1; + } + atomicInteger.getAndIncrement(); + new DownloadThread(cm, this.url, startPos, endPos, "download_file.jpeg", new DownloadListener() { + @Override + public void notifyFinished() { + if (atomicInteger.decrementAndGet() == 0) { + if (FileDownloader.this.listener != null) { + FileDownloader.this.listener.notifyFinished(); + } + } + } + }).start(); + } + } catch (ConnectionException e) { + e.printStackTrace(); + } + } + + public void setConnectionManager(ConnectionManager ucm) { + this.cm = ucm; + } + + public DownloadListener getListener() { + return this.listener; + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } +} \ No newline at end of file diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/FileDownloaderTest.java b/group27/1252327158/task3_20170326/download/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..865d65510d --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.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() { + + String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1489721424&di=1fda6467501ab1d5e5bff43e801d14ee&imgtype=jpg&er=1&src=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201507%2F30%2F20150730163204_A24MX.thumb.700_0.jpeg"; + + 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("下载完成!"); + + + + } + +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/api/Connection.java b/group27/1252327158/task3_20170326/download/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0795bca536 --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/api/Connection.java @@ -0,0 +1,30 @@ +package com.coderising.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(); + + /** + * 获取下载文件的文件名 + * + * @return 文件名 + */ + String getFileName(); +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/api/ConnectionException.java b/group27/1252327158/task3_20170326/download/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..bd8934095f --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,11 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + public ConnectionException(Exception e) { + super(e); + } + + public ConnectionException(String msg) { + super(msg); + } +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/api/ConnectionManager.java b/group27/1252327158/task3_20170326/download/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..69321679fa --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,21 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * + * @param url 连接地址 + * @param startPos 读取文件的起始位置 + * @param endPos 读取文件的结束位置 + * @return 连接 + */ + Connection open(String url, int startPos, int endPos) throws ConnectionException; + + /** + * 获取文件长度 + * + * @param url 连接地址 + * @return 文件长度 + */ + int getContentLength(String url) throws ConnectionException; +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/api/DownloadListener.java b/group27/1252327158/task3_20170326/download/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/impl/ConnectionImpl.java b/group27/1252327158/task3_20170326/download/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..0a367ea1d1 --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,94 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private static final int BUFFER_SIZE = 4096; + private HttpURLConnection httpConn; + private String fileUrl; + private InputStream inputStream; + + public ConnectionImpl(HttpURLConnection httpConn, String fileUrl) { + this.httpConn = httpConn; + this.fileUrl = fileUrl; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + if (endPos < startPos) { + throw new IllegalArgumentException("argument endPos[" + endPos + "] less than startPos[" + startPos + "]"); + } + int bytesNeed2Read = endPos - startPos + 1; + if (bytesNeed2Read > getContentLength()) { + throw new IllegalArgumentException( + "endPos[" + endPos + "] is bigger than content length[" + getContentLength() + "]"); + } + + inputStream = httpConn.getInputStream(); + + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + byte[] buffer = new byte[Math.min(bytesNeed2Read, BUFFER_SIZE)]; + int read; + + long startTime = System.currentTimeMillis(); + final long progressInterval = 2000; + while ((read = inputStream.read(buffer)) != -1) { + byteArrayOutputStream.write(buffer, 0, read); + + if (System.currentTimeMillis() - startTime > progressInterval) { + startTime = System.currentTimeMillis(); + System.out.println(String.format(Thread.currentThread().getName() + + " [%.2f%%]", byteArrayOutputStream.size() * 100.0 / bytesNeed2Read) + ); + } + } + System.out.println(String.format(Thread.currentThread().getName() + " [%.2f%%]", 100.0)); + System.out.println("bytes read: " + byteArrayOutputStream.size()); + + return byteArrayOutputStream.toByteArray(); + } + + @Override + public int getContentLength() { + if (httpConn != null) { + return httpConn.getContentLength(); + } + return 0; + } + + @Override + public void close() { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (httpConn != null) { + httpConn.disconnect(); + } + } + + @Override + public String getFileName() { + String disposition = httpConn.getHeaderField("Content-Disposition"); + if (disposition != null) { + // extracts file name from header field + int index = disposition.indexOf("filename="); + if (index > 0) { + return disposition.substring(index + 10, + disposition.length() - 1); + } + } + // extracts file name from URL + return fileUrl.substring(fileUrl.lastIndexOf("/") + 1, + fileUrl.length()); + } +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/impl/ConnectionManagerImpl.java b/group27/1252327158/task3_20170326/download/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..e88aa35647 --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,61 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String fileURL, int startPos, int endPos) throws ConnectionException { + try { + System.out.println("try to open file url: " + fileURL); + + URL url = new URL(fileURL); + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); + + // 设定读取range + httpConn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + System.out.println("Range: bytes=" + startPos + "-" + endPos); + + int responseCode = httpConn.getResponseCode(); + + System.out.println("server replied HTTP code: " + responseCode); + if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_PARTIAL) { + System.out.println("return new ConnectionImpl"); + return new ConnectionImpl(httpConn, fileURL); + } else { + throw new ConnectionException("server replied HTTP code: " + responseCode); + } + } catch (IOException e) { + throw new ConnectionException(e); + } + } + + @Override + public int getContentLength(String fileURL) throws ConnectionException { + try { + System.out.println("try to open file url: " + fileURL); + + URL url = new URL(fileURL); + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); + int responseCode = httpConn.getResponseCode(); + + System.out.println("server replied HTTP code: " + responseCode); + if (responseCode == HttpURLConnection.HTTP_OK) { + System.out.println("return contentLength: " + httpConn.getContentLength()); + int contentLength = httpConn.getContentLength(); + httpConn.disconnect(); + return contentLength; + } else { + throw new ConnectionException("server replied HTTP code: " + responseCode); + } + } catch (IOException e) { + throw new ConnectionException(e); + } + } +} diff --git "a/group27/1252327158/task3_20170326/\345\217\246\344\270\200\344\270\252linkedlist\344\275\234\344\270\232\345\234\250task1\351\207\214" "b/group27/1252327158/task3_20170326/\345\217\246\344\270\200\344\270\252linkedlist\344\275\234\344\270\232\345\234\250task1\351\207\214" new file mode 100644 index 0000000000..710566766d --- /dev/null +++ "b/group27/1252327158/task3_20170326/\345\217\246\344\270\200\344\270\252linkedlist\344\275\234\344\270\232\345\234\250task1\351\207\214" @@ -0,0 +1 @@ +另一个linkedlist作业在task1里