Skip to content

Instantly share code, notes, and snippets.

@guozi
Forked from rponte/App.java
Created March 10, 2021 07:14
Show Gist options
  • Save guozi/c8e71e362205bd122bee5d70166d69c9 to your computer and use it in GitHub Desktop.
Save guozi/c8e71e362205bd122bee5d70166d69c9 to your computer and use it in GitHub Desktop.

Revisions

  1. @rponte rponte revised this gist Mar 27, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Downloader.java
    Original file line number Diff line number Diff line change
    @@ -17,10 +17,10 @@ public class Downloader {

    public File download(URL url, File dstFile) {
    CloseableHttpClient httpclient = HttpClients.custom()
    .setRedirectStrategy(new LaxRedirectStrategy())
    .setRedirectStrategy(new LaxRedirectStrategy()) // adds HTTP REDIRECT support to GET and POST methods
    .build();
    try {
    HttpGet get = new HttpGet(url.toURI());
    HttpGet get = new HttpGet(url.toURI()); // we're using GET but it could be via POST as well
    File downloaded = httpclient.execute(get, new FileDownloadResponseHandler(dstFile));
    return downloaded;
    } catch (Exception e) {
  2. @rponte rponte revised this gist Mar 12, 2015. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions App.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    public class App {

    public static void main(String[] args) throws MalformedURLException {

    URL rightUrl = new URL("http://cursos.triadworks.com.br/assets/css/main.css");
    URL redirectableUrl = new URL("http://www.triadworks.com.br/assets/css/main.css"); // redirected to cursos.triadworks.com.br

    Downloader downloader = new Downloader();

    System.out.println("Downloading file through right Url...");
    downloader.download(rightUrl, new File("main-ok.css"));

    System.out.println("Downloading file through a redirectable Url...");
    downloader.download(redirectableUrl, new File("main-redirected.css"));

    }
    }
  3. @rponte rponte created this gist Mar 12, 2015.
    50 changes: 50 additions & 0 deletions Downloader.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import org.apache.commons.io.FileUtils;
    import org.apache.commons.io.IOUtils;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.impl.client.LaxRedirectStrategy;

    public class Downloader {

    public File download(URL url, File dstFile) {
    CloseableHttpClient httpclient = HttpClients.custom()
    .setRedirectStrategy(new LaxRedirectStrategy())
    .build();
    try {
    HttpGet get = new HttpGet(url.toURI());
    File downloaded = httpclient.execute(get, new FileDownloadResponseHandler(dstFile));
    return downloaded;
    } catch (Exception e) {
    throw new IllegalStateException(e);
    } finally {
    IOUtils.closeQuietly(httpclient);
    }
    }

    static class FileDownloadResponseHandler implements ResponseHandler<File> {

    private final File target;

    public FileDownloadResponseHandler(File target) {
    this.target = target;
    }

    @Override
    public File handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    InputStream source = response.getEntity().getContent();
    FileUtils.copyInputStreamToFile(source, this.target);
    return this.target;
    }

    }

    }