Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic File upload in GWT

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)

like image 502
Maksim Avatar asked Jul 10 '09 18:07

Maksim


2 Answers

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{     public void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {         ServletFileUpload upload = new ServletFileUpload();          try{             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:

<servlet>     <servlet-name>fileUploaderServlet</servlet-name>     <servlet-class>com.testapp.server.FileUpload</servlet-class> </servlet> <servlet-mapping>   <servlet-name>fileUploaderServlet</servlet-name>   <url-pattern>/testapp/fileupload</url-pattern> </servlet-mapping> 

3) And for form.action did this:

form.setAction(GWT.getModuleBaseURL()+"fileupload"); 
like image 65
KevMo Avatar answered Sep 24 '22 00:09

KevMo


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.

like image 20
rluba Avatar answered Sep 25 '22 00:09

rluba