Uploading a File via Web Browser (Using a Servlet)

Often you need to upload a file to a website or web server. To do this you need to create a form in HTML and submit it to the web server. Here is a short tutorial on how to do it using a Java Servlet to handle the request. Before you try this, make sure you have a good understanding of how to handle Java Web Servlets.

HTML Form

The above HTML allows you create a form which lets you choose a file from your computer to upload to the browser. When the user clicks submit the file is sent to the servlet found at “upload-servlet” specified in the action portion of the HTML form  action="upload-servlet" .

Servlet

To handle the form request you need something on the server to handle the request and do something with it. In this case, write the file to a local directory the server. While there are many server side applications and technologies that can perform this operation, in this case I am using a Java Servlet as it is relevant to a project I am currently working on.

Here we are using annotations to specify the URL to access this servlet. This tutorial does not cover how to set-up and manage servlets. If you do not yet know how to use annotations, or how to configure the web.xml file, I would suggest you find yourself a tutorial or book on how to use Java Servlets to fully understand how this process works.

When you upload the file to the webserver using the HTML form, the file is uploaded in parts. Using the Java Servlet we can access those parts and write them to a file on the webserver, and servlets provide convenience methods to help you do this. However, before we can do that, we need to specify or create a directory where the file should be written.

Here we have simply used the absolute path of the application and appended a directory of our choosing to the end of it. UPLOAD_DIR is just a static string we have specified earlier in the document.

Now that we have a location to write to we want to get the FileName of the document and reconstitute the document to the destination file.

Here we iterate through the parts from the request object, each time writing the part to the destination file. We also get the file name from the part and add that to our path. To do this we have used a utility method  getFileName(part) .

This method extracts the filename from the part’s header, which consists of key-value pairs. It splits the content and iterates through until it find the “filename” key, and then retrieves the value.

Now that we have the filename, and have written the parts to a local file. We can now call what ever Java object we need to process the file. Or we can just leave it there and wait for the user to choose to do something with it.

All that is left to do now is confirm the upload by sending a response back through the webserver.

Here we create and set the attribute “message” and assign it a confirmation string. Then we dispatch/forward the request to another jsp.

Response

Now that we have forwarded the request to the “upload-response.jsp”, all that is left to do is retrieve the message forwarded by the servlet. This can be done using the JSP expression language or “EL” for short.

Here we retrieve the “message” attribute and display it’s value on the JSP page. And voila.

References:

Java Upload File Example with Serlvet 3.0 (www.codejava.net)

Comments are closed.

Powered by WordPress. Designed by Woo Themes