Skip to content

Instantly share code, notes, and snippets.

@rponte
Last active December 23, 2022 08:52
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rponte/09ddc1aa7b9918b52029 to your computer and use it in GitHub Desktop.
Save rponte/09ddc1aa7b9918b52029 to your computer and use it in GitHub Desktop.
Downloading file using Apache HttpClient (>= v4.2) with support to HTTP REDIRECT 301 and 302 when using HTTP method GET or POST
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"));
}
}
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()) // adds HTTP REDIRECT support to GET and POST methods
.build();
try {
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) {
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;
}
}
}
@cpemaratech
Copy link

It helped 👍

@CheongbinKim
Copy link

It helped +1

@teagithub
Copy link

teagithub commented Jul 13, 2019

Thank you!
Tips for others who want to use it: if use maven , pom.xml needs to add the following from https://mvnrepository.com/artifact/org.apache.directory.studio/org.apache.commons.io

@AyeshW
Copy link

AyeshW commented Oct 15, 2019

It worked perfectly. Thank you!

@Harshad-Dange
Copy link

Harshad-Dange commented Jun 27, 2022

I have been looking for this since so long, and finally found it. My use case was to store api response into zip file as its in byte array but was not able to store it into .zip in RestAssured.
If any one knows the alternative for this in RestAssured would be helpful.
Thanks in Advance !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment