The Google Web Toolkit provides the FormPanel and FileUpload classes to handle form and files submission in a rather elegant way. However, the way it works is not that straightforward when reading the API documentation. Worse, I did not find that many resources related to that topic on the web. So here is a quick overview of what you need to handle file uploads using the Google Web Toolkit.

First, you will need to add handlers:

uploadSubmitButton.addClickListener(new ClickListener() {
	public void onClick(Widget sender)
	{
		uploadForm.submit();
	}
});

uploadForm.addFormHandler(new FormHandler() {
	public void onSubmit(FormSubmitEvent event)
	{
		if (fileUpload.getFilename().length() == 0)
		{
			Window.alert("You did not specify a script filename!");
			event.setCancelled(true);
		}
		else if (!fileUpload.getFilename().endsWith(".js"))
		{
			Window.alert("Please specify a JavaScript file ending with '.js'.");
			event.setCancelled(true);
		}
	}

	public void onSubmitComplete(FormSubmitCompleteEvent event)
	{
		Window.alert(event.getResults());
	}
});

Your form probably has a submit button, so all you need is to catch the click event and ask the form to be submitted. In turn, the form can trigger two events:

  1. onSubmit which can be used to perform some client-side validation[1]
  2. onSubmitComplete which is called when the submission has been done.

The tricky part is on onSubmitComplete: event.getResults() corresponds to the server-returned HTTP response body. This means that handling file uploads is quite simple, the server-side logic needs to return a response in a text/plain document. This way, you can simply return some string like OK if the upload succeeded, and other strings for failures. Your GWT code has just to read this response using event.getResults().

Doing the server-side with Java Servlets is a breeze. Let us write a TestCaseUploadServlet servlet in a module.server package of your GWT module. Don't forget to modify your GWT module XML descriptor to mount the servlet:

<!  Servlets and GWT RPC services. >
<servlet path="/testcaseUpload" class="gwt.server.TestCaseUploadServlet"/>

Grab commons-fileupload and commons-io, and add these libraries to your project. Then you can quickly craft a Servlet like the following one (please note that this one is potentially insecure and was developped for quick tests!):

public class TestCaseUploadServlet extends HttpServlet
{

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		response.setContentType("text/plain");
		
		FileItem uploadItem = getFileItem(request);
		if (uploadItem == null)
		{
			response.getWriter().write("NO-SCRIPT-DATA");
			return;
		}
		
		response.getWriter().write(new String(uploadItem.get()));
	}
	
	private FileItem getFileItem(HttpServletRequest request)
	{
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		
		try
		{
			List items = upload.parseRequest(request);
			Iterator it = items.iterator();
			while (it.hasNext())
			{
				FileItem item = (FileItem) it.next();
				if (!item.isFormField() && "testcaseFile".equals(item.getFieldName()))
				{
					return item;
				}
			}
		}
		catch (FileUploadException e)
		{
			return null;
		}
		return null;
	}

}

Have fun ;-)

Notes

[1] Remember that you cannot rely on this on the server-side! Never trust data that comes from the wild!