1

I want to download a file from a third party rest end-point in java

Sample CURL available from google and it is working as expected but I want to design it in java. I tried googling it but could not get help.

Request method is "GET" and a multipart HTTP request

curl -L -O -k -u 'username:password' -X GET http://localhost:8080/secure/attachment/1461863/fileName.txt

Any sample code or link will do good.

Thanks

1 Answers1

0

If you don't want to use additional dependecies other than jdk, you can use something like this:

URL url = new URL("http://localhost:8080/secure/attachment/1461863/fileName.txt");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");

    if (conn.getResponseCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
    }
JArgente
  • 2,239
  • 1
  • 9
  • 11