Skip to main content

How to upload a file using JSP/Servlets

This short article describes how to upload a file using Apache Jakarta commons upload library.

1) Required downloads: Download the library from here. Keep the following JAR files in classpath: commons-fileupload-1.2.1.jar and commons-io-1.4.jar.

2) JSP/HTML code: To upload a file you need to set the encoding type in your form to multipart/form-data.

Give your users a file-select box to select the file :
input type="file" name="myFile" width="100"
You will most probably have other form fields like textbox, radio buttons etc. We have one text field with name 'user' in this form:
input maxlength="255" size="40" name="user"

3) Form data handling: Now you have sent the file and other form data, now we need to make use of this form data. Most probably you need to save the file at some location on the server.
One problem that many developers face is getting a null when they try to extract the value in form fields using request.getParameter(). So in our case, if you try doing this:
String user=request.getParameter("user");
you will find that 'user' has a null value.
But Apache Jakarta Commons Upload library gives us the way to overcome this problem also.
To handle the form data, I am using a servlet here. You may use a JSP, if you want, however that won't be a good design!

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//Step 1: check if the request is a file upload request or not.
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
//Step 2: parse the request.

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = new ArrayList();
try {
items = upload.parseRequest(request);
} catch (FileUploadException fue) {
//Step 3:

Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem);
if (!item.isFormField()) {
// I am saving the files in WEB-INF/images folder.
try {
String FOLDER = getServletContext().getRealPath("/WEB-INF");
String imgName= item.getName();
int last=imgName.lastIndexOf("\\");
FOLDER = FOLDER.replace("WEB-INF", "images/"+imgName);
if (FOLDER != null) {
FOLDER = FOLDER.replace('\\', '/'). replace('/', File.separatorChar);
File file = new File(FOLDER);

} catch (Exception e) {
} else { //Handle the textual form data.
//Get the name of the field

String name = item.getFieldName();
//Get the value of the field

String value = item.getString();
System.out.println("Name=" + name + " value=" + value);

System.out.println("User ="+value);
This is all that you need to upload a file. See it was so simple!


AnilReddy Karna said…
in your code what is qb,and where the object is created

File file = new File(FOLDER);
D.M. Olle said…
Thanks a bunch for was a life saver...!!!!

Popular posts from this blog

How to upload to Google Cloud Storage buckets using CURL

Signed URLs are pretty nifty feature given by Google Cloud Platform to let anyone access your cloud storage (bucket or any file in the bucket) without need to sign in.

Official documentation gives step by step details as to how to read/write to the bucket using gsutil or through a program. This article will tell you how to upload a file to the bucket using curl so that any client which doesn't have cloud SDK installed can do this using a simple script. This command creates a signed PUT URL for your bucket. gsutil signurl -c 'text/plain' -m PUT serviceAccount.json gs://test_bucket_location
Here is my URL:…

File upload problem: UTF-8 encoding not honored when form has multipart/form-data

The problem that I was facing was something like this. I was using Apache Commons File Upload library to upload and download some file.

I had a form in which user can upload a file and another field 'name' in which she can give any name to the file being loaded.

When I submitted the form, the file was uploaded fine but the value in name field was garbled. I followed all the possible suggestions I found:

<%@page pageEncoding="UTF-8"%> set. <%@page contentType="text/html;charset=UTF-8"%gt; set after the first directive. <meta equiv="Content-Type" content="text/html;charset=UTF-8"> in the head. enctype="multipart/form-data" attribute in the form. accept-charset="UTF-8" attribute in the form.
in the Servlet:
before doing any operations on request object: request.setCharacterEncoding("UTF-8"); For accessing the value

FileItem item = (FileItem);

if (item.isFormField()) {

//For regular…

Uploading and Retrieving images on Google Cloud Storage

You would already be aware that there are multiple options given by Google Cloud Platform to store data. Here is Google documentation on when to use which option: Google recommends using Google Cloud Storage (GCS) to store static content like files/videos etc. There is something called 'Blobstore' as well which is also used to store such content but it is on the way to being deprecated. This page talks about using GCS to store images. Look at this page to understand basic requirements for setup of GCS. In the Cloud Store Browser below following buckets are already available. If you select any bucket, you would be able to see the objects created in it.  Here you can see the image file in the '' bucket. You won't be able to add/delete files or folder from the browser if you don't have proper access but through code (running with the service account) it should not be a problem. Objects on GCS are immutable so you can't edit an ob…