How to Build HttpClient In Java to Download File

Building HTTP Client to Download Files such as Image and PDF.IntroductionIn this article, we will build HTTP Client that will download files from the internet.We will download image files and pdf files for this demo but we can extend it to any type of …


This content originally appeared on Level Up Coding - Medium and was authored by Suraj Mishra

Building HTTP Client to Download Files such as Image and PDF.

Introduction

  • In this article, we will build HTTP Client that will download files from the internet.
  • We will download image files and pdf files for this demo but we can extend it to any type of file.
  • FYI, this code requires Java 11 since we will use HttpClient which was added in Java 11.

Building Request Object

  • At first, we will build HTTP GET request. HttpRequest follows the builder pattern.

Building HttpClient Object

  • After that, we will build httpclient and execute to get the response. Since the response consists of files we need to pass bodyhandlers as of InputStream to read the file.

Writing to Local File

  • Writing to a local file is easy, we can write using FileOutputStream. Here we are getting content as input stream.

Download Image Client

  • We are downloading endpoint “ https://miro.medium.com/max/864/1*EDEtjTqyDwAvqdAiMIWXww.png"
  • Since we are downloading images, the response body contains inputstream.
  • Once we have inputstream, we can write to the local disk.

Download PDF Client

  • Similarly, we can also download PDF files, Code will look exactly the same as of Image file.

Conclusion

  • In this article we used Java 11 HttpClient to download images and pdf file. BodyHandler provides different ways to handle response body, and in the case of files we can use InputStream
  • We can download any type of file using this approach.
Let’s connect on LinkedIn
If you like this article, please check more @ https://asyncq.com/

How to Build HttpClient In Java to Download File was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Suraj Mishra


Print Share Comment Cite Upload Translate Updates
APA

Suraj Mishra | Sciencx (2022-11-17T21:49:10+00:00) How to Build HttpClient In Java to Download File. Retrieved from https://www.scien.cx/2022/11/17/how-to-build-httpclient-in-java-to-download-file/

MLA
" » How to Build HttpClient In Java to Download File." Suraj Mishra | Sciencx - Thursday November 17, 2022, https://www.scien.cx/2022/11/17/how-to-build-httpclient-in-java-to-download-file/
HARVARD
Suraj Mishra | Sciencx Thursday November 17, 2022 » How to Build HttpClient In Java to Download File., viewed ,<https://www.scien.cx/2022/11/17/how-to-build-httpclient-in-java-to-download-file/>
VANCOUVER
Suraj Mishra | Sciencx - » How to Build HttpClient In Java to Download File. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/17/how-to-build-httpclient-in-java-to-download-file/
CHICAGO
" » How to Build HttpClient In Java to Download File." Suraj Mishra | Sciencx - Accessed . https://www.scien.cx/2022/11/17/how-to-build-httpclient-in-java-to-download-file/
IEEE
" » How to Build HttpClient In Java to Download File." Suraj Mishra | Sciencx [Online]. Available: https://www.scien.cx/2022/11/17/how-to-build-httpclient-in-java-to-download-file/. [Accessed: ]
rf:citation
» How to Build HttpClient In Java to Download File | Suraj Mishra | Sciencx | https://www.scien.cx/2022/11/17/how-to-build-httpclient-in-java-to-download-file/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.