Download a file using java io






















This article will help you understand them with the help of examples. We will begin by using BufferedInputStream and Files. Next we will see how to achieve the same using Java NIO package. Also, we will see how to use HttpClient , which provides a Non-Blocking way of downloading a file. Finally, we will use third party library of Apache Commons IO to download a file.

First, we will see an example of using Java IO to download a file. Here, we are using BufferedInputStream to download a file. Then, we opened an InputStream from the file using openStream method. Next, in order to be able to download large files we wrapped the input stream into a BufferedInputStream.

Also, we created a FileOutputStream by providing a path on the disk where we want the file to be saved. Next, we use a bucket of byte[] to read bytes from the input stream and writing onto the output stream iteratively. This example, demonstrates how we can use our own buffer for example bytes so that downloading large files should not consume huge memory on our system. To do that, we have used try-with-resources block for respective streams instantiation. While writing the previous example, we had to take care of a lot of logic.

Thankfully, Java Files class provides the copy method which handles these logic internally. The Java NIO package offers a faster way of data transfer, which does not buffer data in memory. Hence, we can easily work with large files. In order to use Java NIO channels, we need to create two channels.

One channel will connect to the source and other to the target. The transferFrom and transferTo methods are much more efficient than working with streams using a buffer. The transfer methods enable us to directly copy the contents of the file system cache to the file on the system. Thus direct channeling restricts the number of context switches required and enhances the overall code performance.

Now, in the following sections, we will be looking at ways to download files from a URL using third-party libraries instead of core Java functionality components. Now you may be thinking why would we use this when Java has its own set of libraries to handle IO operations. However, Apache Commons IO overcomes the problem of code rewriting and helps avoid writing boilerplate code. In order to start using the Apache Commons IO library, you will need to download the jar files from the official website.

When you are done downloading the jar files, you need to add them to use them. If you are using an Integrated Development Environment IDE such as Eclipse , you will need to add the files to the build path of your project. There is only a single line of code required to download a file, which looks like:. The connection and read timeouts convey the permissible time for which either the connection may stay idle or reading from the URL may stop.

We will use the copy inputStream, fileOS method to download a file into the local system. Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

The function returns the number of bytes copied. If the value of the variable i is -1, then it indicates that the contents of the file are over 2GB. When the returned value is -1, you can use the function copyLarge inputStream, fileOS in place of the copy inputstream, fileOS function to handle this load.

Both of these functions buffer the inputstream internally. The internal buffer means we do not have to use the BufferedInputStream class to enhance our code performance and helps us avoid writing boilerplate code. Another library managed by the Apache organization is the HttpComponents package.

This library uses the request-response mechanism to download the file from a given URL. The first step to downloading a file is to create an HTTP client object that would issue the request to the server. For this, we will be using the CloseableHttpClient class. The code snippet that creates a new HTTP client is as follows:. We then need to create an HttpGet or HttpPost object to send the request to the server. The request is created by the following line of code:.

The execute request function is applied to the client object and returns with a response from the server. Once the request is sent to the server we need a response object to receive the data sent from the server. To catch the response from the server we use the HttpResponse class object.

The data sent by the server in the form of a message is obtained through the getEntity function. You can also obtain the response code sent by the server through the response object and use it to your specific need. The data to be downloaded is encapsulated within the entity object and can be extracted using the getContent function.

The getContent function returns an InputStream object that can be further used with a BufferedInputStreamReader to enhance performance. Now all you need to do is read from the stream byte by byte and write the contents into a file using the FileOutputStream class. The last thing required to be done is closing all the open resources in order to ensure that the system resources are not overutilized and that there are no memory leaks.



0コメント

  • 1000 / 1000