Take the 2-minute tour ×

I'm trying to figure out how to upload one file using GWTs FileUpload widget. I'm using GWT and Google AppEngine with Java but I would like to upload file to my own Linux server. I have the following code already but now I can't figure out how to submit my file to the Google AppServer server and save it to another server:

public class FileUploader{

    private ControlPanel cp;
    private FormPanel form = new FormPanel();
    private FileUpload fu =  new FileUpload();

    public FileUploader(ControlPanel cp) {
    	this.cp = cp;

    public Widget getFileUploaderWidget() {
    	// form.setAction(/* WHAT SHOULD I PUT HERE */);

    	VerticalPanel holder = new VerticalPanel();

    	holder.add(new Button("Submit", new ClickHandler() {
    		public void onClick(ClickEvent event) {
    			GWT.log("You selected: " + fu.getFilename(), null);

    	form.addSubmitHandler(new FormPanel.SubmitHandler() {
    		public void onSubmit(SubmitEvent event) {
    			if (!"".equalsIgnoreCase(fu.getFilename())) {
    				GWT.log("UPLOADING FILE????", null);
                                        // NOW WHAT????
    				event.cancel(); // cancel the event


    	form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
    		public void onSubmitComplete(SubmitCompleteEvent event) {


    	return form;

Now, what do I need to do next? What do i need to put in web.xml and how do I write my servlet so i can store file and return url of that object (if possible)

share|improve this question
add comment

6 Answers

up vote 42 down vote accepted

Here's the code from my app:

1) I created a class to accept http request:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.apache.commons.fileupload.FileItemIterator; 
import org.apache.commons.fileupload.FileItemStream; 
import org.apache.commons.fileupload.servlet.ServletFileUpload; 

public class FileUpload extends HttpServlet{
        ServletFileUpload upload = new ServletFileUpload();

            FileItemIterator iter = upload.getItemIterator(request);

            while (iter.hasNext()) {
                FileItemStream item = iter.next();

                String name = item.getFieldName();
                InputStream stream = item.openStream();

                // Process the input stream
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int len;
                byte[] buffer = new byte[8192];
                while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
                    out.write(buffer, 0, len);

                int maxFileSize = 10*(1024*1024); //10 megs max 
                if (out.size() > maxFileSize) { 
                    throw new RuntimeException("File is > than " + maxFileSize);
        catch(Exception e){
            throw new RuntimeException(e);


2) Then in my web.xml I've added these lines:


3) And for form.action did this:

share|improve this answer
I have the exact as the OP: The project is hosted on GAE and I want to upload the file to my own Linux web server. The OP did choose your answer as the best answer so I guess it did help him with his question but I don't know where to put the FileUpload class, is it in the GAE along with the other project files? What about the Linux server? How will it receive the files? And where is the address specified? And the file name? I'm confused by this piece of code. –  Leo Jweda Feb 7 '10 at 18:48
Isn't 10 MiB "10*1024*1024" not "10*(1024*2)", or is "megs" an alternate name for 2 KiB ? –  Stein G. Strindhaug Feb 16 '11 at 14:36
10*(1024*2) is 20Kb, but not 10Mb –  Antonio May 1 '11 at 8:30
+1, but I'd have liked you to add the imports so I didn't have to google every single one. They are: import java.io.InputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItemIterator; import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.FileUploadException; –  mgiuca May 22 '11 at 5:28
I think you forgot to say that this JAR should be included in the class path: commons.apache.org/fileupload/download_fileupload.cgi –  Erel Segal Halevi Jun 20 '12 at 14:27
add comment

I would suggest using GWTUpload because it's dead simple to use and extend. You can add it to your project in less than 10 minutes and it supports GAE right out of the box (using GWTUpload-GAE). See the examples for some common usage scenarios.

share|improve this answer
Note: GWTUpload requires sessions –  Riley Lark May 2 '12 at 16:37
What are the pros/cons of using GWTUpload over the solution offered by @KevMo –  kroiz May 13 '12 at 14:07
GWTUpload allows asynchronous uploads with status updates, multi-file uploads, and much more. It seems to be a full replacement for common Flash alternatives. –  racha May 14 '12 at 6:16
add comment

In GWT, you can post the file to the server using http form methods, and you must use the supplied HttpServlet to accept and save the data as binary blogs in the Appengine BigTable.

Then, you need a second HttpServlet to read the file from bigtable, SET THE MIME TYPE IN THE HTTP HEADER {and caching options}, and then stream the file to the user.

Although RPC isn't NECESSARILY needed, you must let the client know what the generated fileId is so they can access it {unless you want to let user's supply the id and force them to worry about name overrides... ...ick}. Either you can use rpc to ask for a list of / single id {like "newest file id by user"}, or you can return that id in the body of the UploadServlet's response... but then you must make sure your post target is an in-page iframe, poll to make sure the iframe has a body between the submit event and the actual server response, and then parse and use that id in gwt to create an img or object tag that uses the file.

The key part is having one servlet for upload, and another to download. Remember, BigTable just stores binary blobs, so you also need your data entity to have a mime/content Type that can be read from the input file {never rely on file extensions!}. Also, there's a 1MB per entity in the BigTable, and a 10MB request limit for free accounts. You may wish to have your data entity contain a list of 1-10 blobs, each of which are a max 1024bytes.

Basically, your best bet is to find a working, free copy, like Google File Service, and extend it to learn how the system works.

If you wish, I will be posting my own open-source version of file handling, once I finish the gwt control widgets and can consider it all stable enough to be useful to anyone. Email x AT aiyx DOT info if you want me to send you a jar of betalicious code.

share|improve this answer
add comment

I hope it helps

GWT-Google-app-engine for File upload.


share|improve this answer
Hi Tiger, there he is talking about RPC calls and sending objects back and forth. What i want here is to upload file to the server and do "magic" with it. –  Maksim Jul 10 '09 at 19:44
add comment

Unless you are already using other frameworks, I'd strongly suggest using plain vanilla GWT and it's native components. If you use other frameworks, you can explode the size of your application greatly.

Using the native components can be done in 3 steps:

  1. Create a file upload servlet
  2. Modify web.xml
  3. Make a GWT Upload Form

Interestingly enough, the GWT part is the easiest. You can copy my code at GWT Upload in 3 easy steps if you'd like. Happy uploading!

share|improve this answer
Basically I don't really see the difference to stackoverflow.com/a/1111606/787660 thus the downvote because I think the answer is mostly similar but the other one isn't linking somewhere else... –  Chris Mar 29 '13 at 15:48
add comment

Here you go with a complete GWT fileupload with Progress bar

enter image description here

Here you can DOWNLOAD the source

share|improve this answer
add comment

Your Answer


