项目作者: rameezusmani

项目描述 :
Java based HTTP Client library and a pause/resume supported multi threaded HTTP download manager library
高级语言: Java
项目地址: git://github.com/rameezusmani/httplib.git
创建时间: 2020-04-30T12:21:05Z
项目社区:https://github.com/rameezusmani/httplib

开源协议:

下载


httplib

Java based HTTP Client library and a pause/resume supported multi threaded HTTP download manager library

HOW IT IS DIFFERENT

  • You have a complete download manager built into the library
  • Download large files and library will take care of number of threads to run simultaneously to download files quickly
  • Library will itself detect whether file you are downloading has resume supported or not
  • Only java built in classes are used. No need to download any external library

To understand how pause/resume works using bytes range you can read this:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests

HOW TO USE

  • copy usmani.http and usmani.http.downloadmanager folders in your source code and start using it
Making a simple HTTP request
  1. HttpClient cl=new HttpClient(new java.net.URL("http://rameezusmani.com"));
  2. cl.setRequestMethod(HttpClient.REQUEST_METHOD_GET);
  3. HttpResponse response=cl.executeRequest(true);
  4. String str=response.getAsString();
  5. System.out.println(str);
  6. cl.close();
Download a complete file using DownloadManager
  1. HttpDownloadManager mgr=new HttpDownloadManager();
  2. HttpDownloadFile file=new HttpDownloadFile(new java.net.URL("http://rameezusmani.com"));
  3. file.setDestinationPath("D:\\rameez.html");
  4. file.open();
  5. mgr.setDownloadEventListener(new DownloadEventListener(){
  6. public void start(HttpDownloadFile f) {
  7. System.out.println("started downloading "+f.getURL().toString()+" to "+f.getDestinationPath());
  8. }
  9. public void progress(HttpDownloadFile f, long bn) {
  10. System.out.println("Download progress in "+f.getURL().toString()+"::"+bn+" bytes");
  11. }
  12. public void exception(HttpDownloadFile f, Exception ex) {
  13. System.out.println("Exception in "+f.getURL().toString()+"::"+ex.getMessage());
  14. }
  15. public void complete(HttpDownloadFile f) {
  16. System.out.println("Download completed "+f.getURL().toString()+" to "+f.getDestinationPath());
  17. }
  18. });
  19. mgr.download(file);
Resume a previously paused file download

For example if you were downloading a file that was 20MB large but only 2KB(2048 bytes) was downloaded and then for some reason you aborted the download. If you want to resume that download from after 2KB you can do like this

  1. HttpDownloadManager mgr=new HttpDownloadManager();
  2. HttpDownloadFile file=new HttpDownloadFile(new java.net.URL("http://rameezusmani.com"));
  3. file.setBytesDone(2048) //indicating that 2KB file was already downloaded now start downloading from 2049th byte
  4. file.setDestinationPath("D:\\rameez.html");
  5. file.open();
  6. mgr.setDownloadEventListener(new DownloadEventListener(){
  7. public void start(HttpDownloadFile f) {
  8. System.out.println("started downloading "+f.getURL().toString()+" to "+f.getDestinationPath());
  9. }
  10. public void progress(HttpDownloadFile f, long bn) {
  11. System.out.println("Download progress in "+f.getURL().toString()+"::"+bn+" bytes");
  12. }
  13. public void exception(HttpDownloadFile f, Exception ex) {
  14. System.out.println("Exception in "+f.getURL().toString()+"::"+ex.getMessage());
  15. }
  16. public void complete(HttpDownloadFile f) {
  17. System.out.println("Download completed "+f.getURL().toString()+" to "+f.getDestinationPath());
  18. }
  19. });
  20. mgr.download(file);