AJAX File Upload in Java Web Application using jQuery.AjaxFileUpload plugin
In my previous article on File Upload Example in Servlet & Jsp we learnt how to upload a file into sever via Apache Commons FileUpload plugin, In this article we shall add ajax style feature to our previous example using jQuery.AjaxFileUpload.js plugin.
Modifications
Before getting into the tutorial, I will let you know the small modification which I made in jQuery Form Plugin.
1. By default this plugin allows only images, this is specified in jQuery.AjaxFileUpload.js as below.
valid_extensions : ['gif','png','jpg','jpeg']
So inorder to upload file with other extensions, I have modified this to
valid_extensions : ['gif','png','jpg','jpeg','txt','docx','pdf','mkv']
You can include/remove these extensions according to your requirement
2. When file upload fails this plugin sends two parameter in response, they are
status : Just has the status code, it is set to false when file upload fails
message : This holds the error message.
When file upload is successful, no response is sent back to client. So in order to send an upload success message I have modified the code from
settings.onComplete.apply(element, [response, settings.params]);
to
settings.onComplete.apply(element, [ { status : true, message : 'Your file has been uploaded!' }, settings.params ]);
Now let’s go ahead implement ajax file upload functionality in java web application
Library
Folder structure
Now Create a dynamic web project in Eclipse with following folder structure
JSP
Asynchronous file Upload in Java Web Application
The required jquery files are referenced in the head section of jsp file. Here whenever a file is selected, the script gets fired and triggers the servlet via action parameter, which holds the servlet url. This servlet in turn handles the file upload logic in the server side.
Another must read:
jQuery form validation using jQuery Validation plugin
Servlet
package com.fileupload; import java.io.File; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class UploadFile extends HttpServlet { private static final long serialVersionUID = 1L; private final String UPLOAD_DIRECTORY = "C:/Files"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean isMultipart = ServletFileUpload.isMultipartContent(request); // process only if it is multipart content if (isMultipart) { // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); try { // Parse the request Listmultiparts = upload.parseRequest(request); for (FileItem item : multiparts) { if (!item.isFormField()) { String name = new File(item.getName()).getName(); item.write(new File(UPLOAD_DIRECTORY + File.separator + name)); } } } catch (Exception e) { e.printStackTrace(); } } } }
web.xml
Make sure that the web.xml has following mappings
UploadFile com.fileupload.UploadFile UploadFile /UploadFile index.jsp
Demo
On Running this application
When file upload is successful
When we try to upload a file whose format is not specified in jQuery.AjaxFileUpload.js file
I our next article we shall implement ajax file upload functionality with file upload progress bar.
How can i post extra parameter with file in a form?
Hi Mohaideen, The response.status is always undefined. Do you know why ? Thank-you.
Hi Jamil,I used Jquery for fileupload ( fileupload.js) and the file got uploaded backend am using is struts2. I need to show the thumbnail_url in JSP. Now i got the uploadloaded file name in encrupt format like ( upload_7128fa1b_14a38c275a3__7ffb_00000004.tmp). I don know how to set the url for thumbnail and file ur. Please tell how to handle this struts2. Do i want to set getter/ setter for url in struts2 action? am totally new to jquery.