Coder Social home page Coder Social logo

rameezusmani / httplib Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 31 KB

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

Java 100.00%
java http httpclient downloader filedownloader pauseresume httpbytesrange

httplib's Introduction

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
    HttpClient cl=new HttpClient(new java.net.URL("http://rameezusmani.com"));
    cl.setRequestMethod(HttpClient.REQUEST_METHOD_GET);
    HttpResponse response=cl.executeRequest(true);
    String str=response.getAsString();
    System.out.println(str);
    cl.close();
Download a complete file using DownloadManager
    HttpDownloadManager mgr=new HttpDownloadManager();
    HttpDownloadFile file=new HttpDownloadFile(new java.net.URL("http://rameezusmani.com"));
    file.setDestinationPath("D:\\rameez.html");
    file.open();
    mgr.setDownloadEventListener(new DownloadEventListener(){
        public void start(HttpDownloadFile f) {
            System.out.println("started downloading "+f.getURL().toString()+" to "+f.getDestinationPath());	
        }

        public void progress(HttpDownloadFile f, long bn) {
            System.out.println("Download progress in "+f.getURL().toString()+"::"+bn+" bytes");
        }

        public void exception(HttpDownloadFile f, Exception ex) {
            System.out.println("Exception in "+f.getURL().toString()+"::"+ex.getMessage());
        }

        public void complete(HttpDownloadFile f) {
            System.out.println("Download completed "+f.getURL().toString()+" to "+f.getDestinationPath());			
        }
        
    });
    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

    HttpDownloadManager mgr=new HttpDownloadManager();
    HttpDownloadFile file=new HttpDownloadFile(new java.net.URL("http://rameezusmani.com"));
    file.setBytesDone(2048) //indicating that 2KB file was already downloaded now start downloading from 2049th byte
    file.setDestinationPath("D:\\rameez.html");
    file.open();
    mgr.setDownloadEventListener(new DownloadEventListener(){
        public void start(HttpDownloadFile f) {
            System.out.println("started downloading "+f.getURL().toString()+" to "+f.getDestinationPath());	
        }

        public void progress(HttpDownloadFile f, long bn) {
            System.out.println("Download progress in "+f.getURL().toString()+"::"+bn+" bytes");
        }

        public void exception(HttpDownloadFile f, Exception ex) {
            System.out.println("Exception in "+f.getURL().toString()+"::"+ex.getMessage());
        }

        public void complete(HttpDownloadFile f) {
            System.out.println("Download completed "+f.getURL().toString()+" to "+f.getDestinationPath());			
        }
        
    });
    mgr.download(file);

httplib's People

Contributors

rameezusmani avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.