gwtupload custom package with example/sample application

UPDATED: 14 October 2013
Uploading file(s) on the server is always important part of any web application. Today we are talking about uploading file(s) in GWT application along with the progress bar. Before we begin lets have look at the source of this plugin.

Its originally coded by "Manolo Carrasco". I made few changes regarding maximum upload size in original source code. The plugin works fine but when you want dynamic upload size for your each uploader. This facility not provided in current version. By using my custom gwtupload plugin you'll have that flexibility.

Prerequisites (Available in sample application)
1. gwtupload-custom.jar
2. commons-fileupload-1.2.2.jar
3. commons-io-2.3.jar
4. log4j-1.2.15.jar

I attached sample project at the end of the article. Lets have look at the important snippet of code...
package com.javaquery.client;

import gwtupload.client.IFileInput.FileInputType;
import gwtupload.client.IUploadStatus.Status;
import gwtupload.client.IUploader;
import gwtupload.client.IUploader.OnChangeUploaderHandler;
import gwtupload.client.IUploader.OnFinishUploaderHandler;
import gwtupload.client.IUploader.OnStartUploaderHandler;
import gwtupload.client.MultiUploader;


 * Entry point classes define onModuleLoad().
public class GwtuploadSample implements EntryPoint {
  * This is the entry point method.
 public void onModuleLoad() {
  final Label responseLabel = new Label();
  MultiUploader multiUploader = new MultiUploader(FileInputType.BUTTON);
  multiUploader.addOnChangeUploadHandler(new OnChangeUploaderHandler() {
   public void onChange(IUploader uploader) {
    if(uploader.getStatus() == Status.CHANGED){
     System.out.println("File input changed, can be handled before upload begin");
      * If you want to validate something at your side before upload.  
      * Check it here and you can cancel uploading.
      * Uncomment below line if you need it.
      * /
     /* uploader.cancel(); */
  multiUploader.addOnStartUploadHandler(new OnStartUploaderHandler() {
   public void onStart(IUploader uploader) {
     * This is where you can pass the hidden value to servlet.
    uploader.add(new Hidden("param1", "value"));
    uploader.add(new Hidden("param2", "value"));
  multiUploader.addOnFinishUploadHandler(new OnFinishUploaderHandler() {
   public void onFinish(IUploader uploader) {
     * Before processing output check the status of upload.
    if(uploader.getStatus() == Status.SUCCESS){
      * If you passed details in JSONObject. You have to use subtring function of String to get your JSON String.
  //10485760 = 10 MB x 1024 x 1024
  // Add the nameField and sendButton to the RootPanel
  // Use RootPanel.get() to get the entire body element
package com.javaquery.server;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;

import gwtupload.server.UploadAction;
import gwtupload.server.exceptions.UploadActionException;

public class uploadServlet extends UploadAction{
 private static final long serialVersionUID = -4035393951562844790L;

 public String executeAction(HttpServletRequest request, List sessionFiles) throws UploadActionException {
  String response = "";
  for (FileItem item : sessionFiles) {
   if (false == item.isFormField()) {
    String fileNameWithExt = item.getName();
    String fileName = fileNameWithExt.substring(0, fileNameWithExt.lastIndexOf("."));
    String fileExt = fileNameWithExt.substring(fileNameWithExt.lastIndexOf("."), fileNameWithExt.length());
    String uploadDir = getServletContext().getRealPath("/") + "gwtuploadFiles" + File.separatorChar;
    try {
      * Creating temporary file at "C:\Users\javaQuery\AppData\Local\Temp"
      * File.createTempFile(arg1,arg2)
      * arg1 : prefix you want to set. 
      *      Why "_projectName" used?
      *      - Say if filename length is 2 character then it'll throws error. 
      *        So for safe side we added "_projectName" to ensure that it must be long name.
      * arg2 : postfix you want to set. Whether its and extension or anything you like
     File tempFile = File.createTempFile(fileName+"_projectName", ".bin");

      * Before writing file to server folder you can scan temporary file (with antivirus on server) if you want.
      * Write it to server at your desired location. 
     File createDir = new File(uploadDir);
     if (!createDir.exists()) {
     File saveOnserver = new File(uploadDir + File.separatorChar + item.getName());
      * Copy temporary file to original file.
     FileInputStream inStream = new FileInputStream(tempFile);
     FileOutputStream outStream = new FileOutputStream(saveOnserver);
     byte[] buffer = new byte[1024];
     int length;
     while ((length = > 0) {
      outStream.write(buffer, 0, length);
      * Set response message you want at client side.
      * Say if you want extended details of your upload. Create JSON object, put values in it and pass it as String.
     response += "File saved as " + saveOnserver.getAbsolutePath();
    } catch (Exception e) {
     throw new UploadActionException(e);
     * You can get the other parameter of your request here.
    String hiddenFieldName = item.getFieldName();
    String hiddenFieldValue = item.getString();
    System.out.println("{param: "+hiddenFieldName+"; value: "+hiddenFieldValue+"}");

  // Remove files from session because we have a copy of them

  // Send your customized message to the client.
  return response;

<web-app version="2.5" xmlns:xsi="" xmlns="" xsi:schemalocation=" 
  <!-- Default page to serve -->


Now you are good to go for multiple file upload in GWT with progress bar. Don't forget to check official project website. For any queries and latest updates.

Download Sample Application