|
1 | 1 | package org.wsc.coderising.download.impl;
|
2 | 2 |
|
| 3 | +import java.io.BufferedInputStream; |
| 4 | +import java.io.ByteArrayOutputStream; |
3 | 5 | import java.io.IOException;
|
| 6 | +import java.io.InputStream; |
| 7 | +import java.net.HttpURLConnection; |
4 | 8 |
|
5 | 9 | import org.wsc.coderising.download.api.Connection;
|
| 10 | +import org.wsc.coderising.download.api.ConnectionException; |
6 | 11 |
|
7 | 12 | /**
|
8 | 13 | *
|
|
14 | 19 | *
|
15 | 20 | */
|
16 | 21 | public class ConnectionImpl implements Connection {
|
| 22 | + |
| 23 | + /** 默认缓冲大小 */ |
| 24 | + private final static int DEFAULT_SIZE = 1024; |
17 | 25 |
|
18 |
| - @Override |
19 |
| - public byte[] read(int startPos, int endPos) throws IOException { |
| 26 | + private HttpURLConnection conn; |
| 27 | + |
| 28 | + private InputStream is; |
| 29 | + |
| 30 | + private ByteArrayOutputStream bos; |
20 | 31 |
|
21 |
| - return null; |
| 32 | + @SuppressWarnings("static-access") |
| 33 | + @Override |
| 34 | + public byte[] read(int startPos, int endPos) throws IOException, ConnectionException { |
| 35 | + // 设置读取范围 |
| 36 | + conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); |
| 37 | + conn.setFollowRedirects(true);//自动执行重定向 |
| 38 | + conn.setConnectTimeout(30000);//等待响应时间 |
| 39 | + checkStatus(); |
| 40 | + byte[] buf = new byte[Math.min(getContentLength(), DEFAULT_SIZE)]; |
| 41 | + is = new BufferedInputStream(conn.getInputStream()); |
| 42 | + bos = new ByteArrayOutputStream(); |
| 43 | + int lenth;//实际读取长度 |
| 44 | + //读取 |
| 45 | + while ((lenth = is.read(buf))!= -1) |
| 46 | + bos.write(buf, 0, lenth); |
| 47 | + return bos.toByteArray(); |
22 | 48 | }
|
23 | 49 |
|
24 | 50 | @Override
|
25 | 51 | public int getContentLength() {
|
26 |
| - |
27 |
| - return 0; |
| 52 | + return conn.getContentLength(); |
28 | 53 | }
|
29 | 54 |
|
30 | 55 | @Override
|
31 | 56 | public void close() {
|
| 57 | + if (bos != null) |
| 58 | + try { |
| 59 | + bos.close(); |
| 60 | + } catch (IOException e) { |
| 61 | + e.printStackTrace(); |
| 62 | + } |
| 63 | + if (is != null) |
| 64 | + try { |
| 65 | + is.close(); |
| 66 | + } catch (IOException e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + if(conn != null) |
| 70 | + conn.disconnect(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public String getFileName() { |
| 75 | + String fileName = null; |
| 76 | + String field = conn.getHeaderField("Content-Disposition"); |
| 77 | + if(field == null ){ |
| 78 | + String urlStr = conn.getURL().toString(); |
| 79 | + fileName = urlStr.substring(urlStr.lastIndexOf("/")+1); |
| 80 | + }else{ |
| 81 | + fileName=field.substring(field.indexOf("filename")+10, field.length()-1); |
| 82 | + } |
| 83 | + System.out.println(fileName); |
| 84 | + return fileName; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * 检查连接状态 |
| 89 | + * @throws ConnectionException |
| 90 | + */ |
| 91 | + private void checkStatus() throws ConnectionException { |
| 92 | + try { |
| 93 | + int responseCode = conn.getResponseCode(); |
| 94 | + if (responseCode != HttpURLConnection.HTTP_OK && responseCode != HttpURLConnection.HTTP_PARTIAL) { |
| 95 | + throw new ConnectionException("server response code: " + responseCode); |
| 96 | + } |
| 97 | + } catch (IOException e) { |
| 98 | + throw new ConnectionException(e); |
| 99 | + } |
| 100 | + } |
32 | 101 |
|
| 102 | + public HttpURLConnection getConn() { |
| 103 | + return conn; |
33 | 104 | }
|
34 | 105 |
|
| 106 | + public void setConn(HttpURLConnection conn) { |
| 107 | + this.conn = conn; |
| 108 | + } |
| 109 | + |
| 110 | + public ConnectionImpl(HttpURLConnection conn) { |
| 111 | + super(); |
| 112 | + this.conn = conn; |
| 113 | + } |
| 114 | + |
35 | 115 | }
|
0 commit comments