Uploading and Downloading Files is one of the core functionality that any Enterprise Application wants to incorporate. Our code is built on top of Web Starter Application.
We are using WebJars for Bootstrap and Jquery. At the core of this application will be our service class — FileSystemStorageService. We will look at each of its functions in the rest of the article.
This code is executed after the service class object is created. In this init method, we try to create the directory where we want to upload our files. This method will get a MultipartFile from Spring controller. The file name is then resolved relative to our upload directory and copied there. Above code, converts a file that we want to download into a Resource. This resource is later pushed to download via the controller.
Now let us look at few controller methods which utilize above service class to achieve the functionality. Above method will kick off, when you upload a file from UI. The Spring controller receives a MultipartFile, which is then sent to storage service class. Downloading a file is 2 step process. First, we have to list all the files in the URL form and when the user clicks on any of the links, we will send the actual file. Listing of files uses MvcUriComponentsBuilder to prepare the URL based on the method which is going to actually serve the file for download.
When a user clicks on a file name headers and attachments is sent to the client. Notice that we are allowing only text files to be uploaded. The upload button is enabled only when a user selects text file.
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.
So there you have it - these are the simplest ways to download a file using the basic Java code and other third party libraries. Now that we are done with the basics, you can be as creative as you want and utilize the knowledge to suit your needs.
So see you next time with a new set of concepts to help you become a better coder.
0コメント