Uploading the main file to GWT

I am trying to figure out how to upload a single file using the GWTs FileUpload widget. I use GWT and Google AppEngine with Java, but I would like to upload the file to my own Linux server. I already have the following code, but now I can’t figure out how to send the file to the Google AppServer server and save it on 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 can I write my servlet so that I can store the file and return the URL of this object (if possible)

+44
java google-app-engine file-upload gwt
Jul 10 '09 at 18:13
source share
5 answers

Here is the code from my application:

1) I created a class to accept an 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 added the following 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"); 
+57
Jul 10 '09 at 19:47
source share

I would suggest using GWTUpload because it is dead just for use and expansion. 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 examples for some common use cases .

+11
Jul 30 '10 at 16:10
source share

In GWT, you can send a file to the server using the http form methods, and you must use the provided HttpServlet to receive and save data as binary blogs in Appengine BigTable.

Then you need a second HttpServlet to read the file from the large table, set the MIME type to HTTP HEADER {and cache parameters}, and then transfer the file to the user.

Although RPC is REQUIRED, you must tell the client that the generated fileId is available so that it can access it {if you do not want the user to supply an identifier and make them worry about name rewriting ...... ick} . Either you can use rpc to request the / single id list {for example, "the newest file identifier by user"}, or you can return this identifier in the body of the UploadServlet response ... but then you need to make sure your target post is in- page iframe, poll to make sure the iframe has a body between the submit message and the actual server response, and then parse and use this id in gwt to create an img or object tag that uses this file.

The key part has one servlet for loading, and another for loading. Remember, BigTable just stores binary blobs, so you also need your data object to have a mime / content type that can be read from the input file {never rely on file extensions!}. In addition, BigTable has 1 MB per entity, and 10 MB for free accounts. You may want your data object to contain a list of 1-10 blocks, each of which has a maximum of 1024 bytes.

In principle, it’s best to find a working free copy, such as the Google File Service, and expand it to find out how the system works.

If you want, I will send my own version of open source file processing as soon as I finish the gwt control widgets and can consider everything stable enough to be useful to everyone. Send x AT aYX DOT if you want me to send you a beta code jar.

+5
Aug 12 '09 at 7:34
source share

If you already use other frameworks, I would strongly suggest using simple vanilla GWT and its own components. If you use other frameworks, you can significantly increase the size of your application.

Using your own components can be completed in 3 steps:

  • Create file upload servlet
  • Edit web.xml
  • Make GWT Download Form

Interestingly, the GWT part is the lightest. You can copy the GWT Upload code in 3 easy steps if you want. Happy download!

+2
Jul 09 2018-12-12T00:
source share

Here you will find the full download of the GWT file with a progress bar

enter image description here

Here you can DOWNLOAD the source

+1
Apr 09 '13 at 15:28
source share



All Articles