Last active
July 6, 2016 18:17
-
-
Save huantt/4cd3318c3370ec20d401ffe00e0a541d to your computer and use it in GitHub Desktop.
Download File using MutilThread in java. Mấu chốt là dùng thằng conn.setRequestProperty("Range", "bytes=" + byteRange)
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
package com.huantt.downloader; | |
import java.io.*; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
/** | |
* Created by Huan on 7/5/2016. | |
*/ | |
public class Downloader implements Runnable { | |
private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(Downloader.class); | |
private BufferedInputStream bis; | |
private RandomAccessFile bos; | |
private String link; | |
private String path; | |
private File file; | |
private int start; | |
private int end; | |
public Downloader(String link, String path, File file, int start, int end) { | |
this.link = link; | |
this.path = path; | |
this.start = start; | |
this.end = end; | |
this.file = file; | |
} | |
@Override | |
public void run() { | |
URL url = null; | |
try { | |
url = new URL(link); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
String byteRange = start + "-" + end; | |
connection.setRequestProperty("Range", "bytes=" + byteRange); | |
connection.connect(); | |
bis = new BufferedInputStream(connection.getInputStream()); | |
bos = new RandomAccessFile(file, "rw"); | |
bos.seek(start); | |
byte[] bytes = new byte[1024]; | |
int length; | |
int downloaded = start; | |
try { | |
while (downloaded <= end) { | |
length = bis.read(bytes); | |
downloaded += length; | |
if (length != -1) { | |
bos.write(bytes, 0, length); | |
} else break; | |
log.info("Downloaded : " + downloaded); | |
} | |
bos.close(); | |
log.info("Finish !"); | |
log.info("Downloaded : " + downloaded); | |
} catch (MalformedURLException e) { | |
log.error(e.toString()); | |
} catch (IOException e) { | |
log.error(e.toString()); | |
} | |
} catch (IOException e) { | |
log.error(e.toString()); | |
} | |
} | |
} |
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
package com.huantt.downloader; | |
import java.io.File; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
/** | |
* Created by Huan on 7/6/2016. | |
*/ | |
public class DownloadManager implements Runnable { | |
private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(Downloader.class); | |
int numOfThread; | |
private String link; | |
private String path; | |
private String fileName; | |
private static ExecutorService pool = Executors.newCachedThreadPool(); | |
public DownloadManager(int numOfThread, String link, String path, String fileName) { | |
this.numOfThread = numOfThread; | |
this.link = link; | |
this.path = path; | |
this.fileName = fileName + getFormatFile(); | |
} | |
public DownloadManager(int numOfThread, String link, String path) { | |
this.numOfThread = numOfThread; | |
this.link = link; | |
this.path = path; | |
this.fileName = getFileName(); | |
} | |
private String getFileName() { | |
return new String(link.substring(link.lastIndexOf('/') + 1)); | |
} | |
private String getFormatFile() { | |
String format = ""; | |
if (link.lastIndexOf(".") != -1) { | |
format = link.substring(link.lastIndexOf(".")); | |
} | |
return format; | |
} | |
@Override | |
public void run() { | |
URL url = null; | |
try { | |
url = new URL(link); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
File file = new File(path + "/" + fileName); | |
int sizeFile = connection.getContentLength(); | |
int partSize = sizeFile / numOfThread; | |
int start = 0; | |
int end = partSize; | |
for (int i = 0; i < numOfThread - 1; i++) { | |
log.info("Data of Thread Download " + (i + 1) + ": " + start + "==========>" + end); | |
pool.submit(new Downloader(link, path, file, start, end)); | |
start = end; | |
end += partSize; | |
} | |
end = sizeFile; | |
log.info("Data of Thread Download " + numOfThread + ": " + start + "==========>" + end); | |
pool.submit(new Downloader(link, path, file, start, end)); | |
pool.shutdown(); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
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
package com.huantt.downloader; | |
import org.apache.log4j.xml.DOMConfigurator; | |
/** | |
* Created by Huan on 7/6/2016. | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
DOMConfigurator.configureAndWatch(".\\config\\log4j.xml"); | |
DownloadManager downloadManager = new DownloadManager(2,"http://pets.petsmart.com/services/_images/grooming/dog/dt_t/dog-bath-haircut.jpg","D:/"); | |
Thread thread = new Thread(downloadManager); | |
thread.start(); | |
DownloadManager downloadManager1 = new DownloadManager(2,"http://pets.petsmart.com/services/_images/grooming/dog/dt_t/dog-bath-haircut.jpg","D:/","Tên Ảnh"); | |
Thread thread1 = new Thread(downloadManager1); | |
thread1.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment