Basic File upload in GWT

Posted by Maksim on Stack Overflow See other posts from Stack Overflow or by Maksim
Published on 2009-07-10T18:13:09Z Indexed on 2010/04/26 5:03 UTC
Read the original article Hit count: 374

Filed under:
|
|
|

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;
    	this.cp.setPrimaryArea(getFileUploaderWidget());
    }

    @SuppressWarnings("deprecation")
    public Widget getFileUploaderWidget() {
    	form.setEncoding(FormPanel.ENCODING_MULTIPART);
    	form.setMethod(FormPanel.METHOD_POST);
    	// form.setAction(/* WHAT SHOULD I PUT HERE */);

    	VerticalPanel holder = new VerticalPanel();

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

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

    		}
    	});

    	form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
    		public void onSubmitComplete(SubmitCompleteEvent event) {
    			Window.alert(event.getResults());
    		}
    	});

    	form.add(holder);

    	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)

© Stack Overflow or respective owner

Related posts about gwt

Related posts about fileupload