Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions group09/790466157/src/com/coderising/download/DownloadThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.coderising.download;


import java.io.RandomAccessFile;
import java.util.concurrent.CyclicBarrier;


//import org.omg.CORBA.portable.InputStream;

import com.coderising.download.api.Connection;

public class DownloadThread extends Thread{

Connection conn;
int startPos;
int endPos;
CyclicBarrier barrier ;
String filePath;

public DownloadThread(CyclicBarrier barrier , Connection conn, int startPos, int endPos , String filePath){

this.barrier = barrier;
this.conn = conn;
this.startPos = startPos;
this.endPos = endPos;
this.filePath = filePath;
}
public void run(){
try{
System.out.println("begin download startPos="+startPos+",endPos="+endPos);
byte[] buffer = conn.read(startPos , endPos);
RandomAccessFile file = new RandomAccessFile(filePath, "rw");
file.seek(startPos);
file.write(buffer, 0, buffer.length);
file.close();
barrier.await();
}catch(Exception e){
System.out.println("download error:startPos="+startPos+",endPos="+endPos);
}
}

}
111 changes: 111 additions & 0 deletions group09/790466157/src/com/coderising/download/FileDownloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.coderising.download;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.CyclicBarrier;

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


public class FileDownloader {

String url;

DownloadListener listener;

ConnectionManager cm;

private static final int THREAD_NUM = 3;

private static final String BASE_PATH = "C:/Users/Liner/Desktop/Document/Doc";

boolean isFinished = false;

public FileDownloader(String _url) {
this.url = _url;
File baseFile = new File(BASE_PATH);
if(!baseFile.exists()){
baseFile.mkdirs();
}
}

public void execute(){

CyclicBarrier barrier = new CyclicBarrier(THREAD_NUM, new Runnable() {

@Override
public void run() {
listener.notifyFinished();
}
});

Connection conn = null;
try {

conn = cm.open(this.url);

int length = conn.getContentLength();
String filePath = BASE_PATH + "download."+getFileType(this.url);
//System.out.println(filePath);

File file = new File(filePath);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try{
FileOutputStream fos = new FileOutputStream(file);
fos.write(new byte[length], 0, length);//占位
fos.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
}

int blockSize = (length % THREAD_NUM == 0 ) ? length / THREAD_NUM : (length / THREAD_NUM + 1);
for (int i = 0; i < THREAD_NUM; i++) {
int startPos = i * blockSize;
int endPos = startPos + blockSize - 1;
if(endPos >= length - 1){
endPos = length - 1;
}
new DownloadThread(barrier , conn, startPos, endPos , filePath).start();
}

} catch (ConnectionException e) {
System.out.println(e.getMessage());
} finally{
if(conn != null){
conn.close();
}
}

}

private String getFileType(String url) {
int index = url.lastIndexOf(".");
return url.substring(index + 1 , url.length());
}

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,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=1488796402240&di=8ca9322617d5338cad61232a06f6ed7a&imgtype=0&src=http%3A%2F%2Fjiangsu.china.com.cn%2Fuploadfile%2F2017%2F0212%2F1486868426284307.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("������ɣ�");



}

}
26 changes: 26 additions & 0 deletions group09/790466157/src/com/coderising/download/api/Connection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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;
/**
* �õ��������ݵij���
* @return
*/
public int getContentLength();

/**
* �ر�����
*/
public void close();
public Connection open(Object url);
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.coderising.download.api;

public class ConnectionException extends Exception {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.coderising.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.coderising.download.api;

public interface DownloadListener {
public void notifyFinished();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.coderising.download.impl;


import java.io.IOException;



import org.omg.CORBA.portable.InputStream;

import com.coderising.download.api.Connection;


import java.net.URLConnection;

import com.coderising.download.api.Connection;

public class ConnectionImpl implements Connection{

private URLConnection connection;

@Override
public byte[] read(int startPos, int endPos) throws IOException {
// connection.setAllowUserInteraction(true);
// connection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos);
InputStream inputstream = (InputStream) connection.getInputStream();
byte[] buffer = new byte[endPos - startPos + 1];
inputstream.skip(startPos);
inputstream.read(buffer);
inputstream.close();
return buffer;
}

@Override
public int getContentLength(){
return connection.getContentLength();
}

@Override
public void close() {
}

public void setConnection(URLConnection connection) {
this.connection = connection;
}

@Override
public Connection open(Object url) {
// TODO Auto-generated method stub
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.coderising.download.impl;




import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;




import com.coderising.download.api.Connection;
import com.coderising.download.impl.ConnectionImpl;
import com.coderising.download.api.ConnectionException;
import com.coderising.download.api.ConnectionManager;

public class ConnectionManagerImpl implements ConnectionManager {

@Override
public Connection open(String url) throws ConnectionException {
URL urlObj ;
ConnectionImpl connection = null;
try {
urlObj = new URL(url);
connection = new ConnectionImpl();
} catch (MalformedURLException e) {
throw new ConnectionException();
}
return connection;
}
}