Example of uploading file to the server in servlet

Here, we will learn how to upload file to the server. For uploading a file to the server, method must be post and enctype must be multipart/form-data in html file. For Example:


index.html

Example of uploading file to the server in servlet

Now, for uploading a file to the server, there can be various ways. But, I am going to use MultipartRequest class provided by oreilly. For using this class you must have cos.jar file. If you will download this example, we will the cos.jar file alongwith code.


UploadServlet.java

There are two arguments passed in MultipartRequest class constructor, first one is HttpServletRequest object and second one is String object (for location). Here I am supposing that you have new folder in D driver.


web.xml file

This configuration file provides information about the servlet.