Uploading File to the server using Servlet and JSP is a common task in Java web application. Before coding your Servlet or JSP to handle file upload request, you need to know a little bit about File upload support in HTML and HTTP protocol. If you want your user to choose files from the file system and upload to the server then you need to use <input type="file"/>. This will enable to choose any file from the file system and upload to a server. Next thing is that form method should be HTTP POST with enctype as multipart/form-data, which makes file data available in parts inside request body. Now in order to read those file parts and create a File inside Servlet can be done by using ServletOutputStream. It's better to use Apache commons FileUpload, an open source library. Apache FileUpload handles all low details of parsing HTTP request which conform to RFC 1867 or "Form-based File upload in HTML” when you set form method post and content type as "multipart/form-data".

Apache Commons FileUpload - Important points:

1) DiskFileItemFactory is default Factory class for FileItem. When Apache commons read multipart content and generate FileItem, this implementation keeps file content either in memory or in the disk as a temporary file, depending upon threshold size. By default DiskFileItemFactory has threshold size of 10KB and generates temporary files in temp directory, returned by System.getProperty("java.io.tmpdir"). Both of these values are configurable and it's best to configure these for production usage. You may get permission issues if user account used for running Server doesn't have sufficient permission to write files into the temp directory.

2) Choose threshold size carefully based upon memory usage, keeping large content in memory may result in java.lang.OutOfMemory,while having too small values may result in lot's of temporary files.

3) Apache commons file upload also provides FileCleaningTracker for deleting temporary files created by DiskFileItemFactory. FileCleaningTracker deletes temporary files as soon as corresponding File instance is garbage collected. It accomplishes this by a cleaner thread which is created when FileCleaner is loaded. If you use this feature, then remember to terminate this Thread when your web application ends.

Dependency

In order to compile and run this Java web application in any web server e.g. Tomcat, you need to include following dependency JAR in WEB-INF lib folder.

commons-fileupload-1.2.2.jar

commons-io-2.4.jar

If you are using Maven then you can also use following dependencies :

<dependency>

<groupId>commons-fileupload</groupId>

<artifactId>commons-fileupload</artifactId>

<version>1.2.2</version>

</dependency>

<dependency>

<groupId>commons-io</groupId>

<artifactId>commons-io</artifactId>

<version>2.4</version>

</dependency>

That's all on How to upload Files using Servlet and JSP in Java web application. This File Upload example can be written using JSP, Filter or Servlet because all three are request’s entry point in Java web application. I have used Servlet for handling File upload request for simplicity. By the way from Servlet 3.0 API, Servlet is supporting multipart form data and you can use getPart() method of HttpServletRequest to handle file upload.

Hello ! :)I'm trying to get this upload thing to work for 3 hours now, i'm getting crazy ! Your example seemed very good so I copied it aaaaaand it doesn't work :(I'm getting the message "File Uploaded Successfully" but there's no file where it should...I've found out that the issue is with the line :

I have used the above code. It is working fine if I run it on the Server installed on windows platform, but while executing this on the Unix server, the image is not getting uploaded. Can you please tell me how to upload the image in the unix server?

@Nitish, this is Java code and it's using Java library, so its supposed to work on both Windows and Linux. Can you please tell us more about the issue? are you getting any exception on Linux? What exactly means you are not able to upload images?

Hi , When i get file in spring rest API using Multi-part. I am not storing file on tomcat server . But why is store in my tomcat server root directory by default. Please give me solutions and do want to store file on server. Please help me

Hello @Vaibhav, Your server must be using default location for storing uploaded file. If you want to store file somewhere else, just look for that option in Tomcat. Where exactly you want to store your files?