Let's write an example to upload file using apache common files upload utility.Ĭreate auploadFile.jsp file– This file will display an option to upload the file and will submit the request to processUploadedFile.jsp įile Upload Example using Apache commons File Upload Utility Īdd external jar files in lib directory–Add commons-fileupload.jar and commons-io.jar file in WEB-INF/lib directory of web application.Īdd context param in web.xml– Add context param in web.xml to configure the path where the file will be uploaded and with which name it will be uplaoded. To write file to a directory, we can create a File object and pass it as argument to write() method of FileItem class. Let's discuss each approach in detail 17.2.1 File Upload with Apache Commons File Upload UtilityĪpache Commons file upload utility provides DiskFileItemFactory factorycalss that provides a method to parse the HttpServletRequest object and return list of FileItem class( List of file item because we can upload multiple file in a one go )įileItem provides useful method to get the file name, field name in form, size and content type details of the file that needs to be uploaded. This approach requires below two jar files to be added in WEB-INF/lib directory of web application.(You candownload these jar files from )ī) Servlet 3.0 API - Servlet 3.0Specification provided additional support for uploading files to server and we donot have to include any third party APIs. There are two ways to upload a file to serverĪ) Using third party Jar File – Apache provides a convenient and powerful API to upload the files on server. The browser associates a Browse button with each of them. If we need to upload multiple files uploading, we need to include more than one input tags with different values for the name attribute.
This attribute informs the server that file is uploaded. Additional form attribute (enctype) should be set to multipart/form-data.Remember with GET, data is sent as a query string so we cannot use GET type while uploading files. The form method attribute must be set to POST method and GET method cannot be used.Whenever client needs to upload a file, there are several important points which needs to be keep in mind. Whereas Internet Explorer displays Browse button like On click of “Choose File” button, file selector will be opened with which a file can be selected and on selecting the file, chosen file name will be displayed instead “No file chosen” This element will display “Chose File” button which can be used to select the file from client machine that needs to be uploaded (refer below figure) We can use this element in a form like belowĪll browser displays it slightly different. HTML provides a different type of input element with type “file”. You must have seen this functionality in several website like uploading resume on job portal. We have seen several examples in this tutorial where client sends text data but we can upload text, binary, image or any other type of files on server as well.