0% found this document useful (0 votes)
1K views3 pages

Usage of GWTUpload Library

1. The document describes how to use the GWTUpload library to add file upload capabilities to a GWT application. 2. It involves editing the module configuration file to include GWTUpload and the entry point class. The web.xml must be edited to define servlet mappings for the upload handler. 3. The client application creates an uploader panel and handles upload finish events to process the uploaded files.

Uploaded by

jonhkr
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views3 pages

Usage of GWTUpload Library

1. The document describes how to use the GWTUpload library to add file upload capabilities to a GWT application. 2. It involves editing the module configuration file to include GWTUpload and the entry point class. The web.xml must be edited to define servlet mappings for the upload handler. 3. The client application creates an uploader panel and handles upload finish events to process the uploaded files.

Uploaded by

jonhkr
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Usage of GWTUpload library.

Also, add these libraries to your application: commons-fileupload-1.2.jar,


commons-io-1.3.1.jar and log4j.jar

1. Edit your module file: Xxx.gwt.xml.

<module>

  <!-- Include GWTUpload library -->


  <inherits name="gwtupload.GWTUpload"/>
  <!-- Load dinamically predefined styles in the library when
the application starts -->
  <stylesheet src="Upload.css"/>
   
  <!-- Change this line with your project's entry-point -->
  <entry-point class="package.Xxx"/>
</module>
2. Edit your web.xml and include your customized servlet

<context-param>
    <!-- max size of the upload request -->
    <param-name>maxSize</param-name>
    <param-value>3145728</param-value>
  </context-param>

  <context-param>
    <!-- Useful in development mode to slow down the uploads in
fast networks.
         Put the number of milliseconds to sleep in each block
received in the server.
         false or 0, means don't use slow uploads  -->
    <param-name>slowUploads</param-name>
    <param-value>200</param-value>
  </context-param>

  <servlet>
    <servlet-name>uploadServlet</servlet-name>
    <!-- put here your customized servlet extending UploadAction
-->
    <servlet-class>gwtupload.server.UploadServlet</servlet-
class>
  </servlet>

  <servlet-mapping>
    <servlet-name>uploadServlet</servlet-name>
    <url-pattern>*.gupld</url-pattern>
  </servlet-mapping>

3. Create your client application


 
/**
 * An example of a MultiUploader panel using a very simple
upload progress widget
 * The example also uses PreloadedImage to display uploaded
images.
 *
 * @author Manolo Carrasco Moñino
 */
public class MultipleUploadSample implements EntryPoint {

  // A panel where the thumbnails of uploaded images will be


shown
  private FlowPanel panelImages = new FlowPanel();

  public void onModuleLoad() {


    // Attach the image viewer to the document
    RootPanel.get("thumbnails").add(panelImages);
   
    // Create a new uploader panel and attach it to the document
    MultiUploader defaultUploader = new MultiUploader();
    RootPanel.get("default").add(defaultUploader);

    // Add a finish handler which will load the image once the
upload finishes
   
defaultUploader.addOnFinishUploadHandler(onFinishUploaderHandler
);
  }

  // Load the image in the document and in the case of success


attach it to the viewer
  private IUploader.OnFinishUploaderHandler
onFinishUploaderHandler = new
IUploader.OnFinishUploaderHandler() {
    public void onFinish(IUploader uploader) {
      if (uploader.getStatus() == Status.SUCCESS)
        new PreloadedImage(uploader.fileUrl(), showImage);
    }
  };

  // Attach an image to the pictures viewer


  private OnLoadPreloadedImageHandler showImage = new
OnLoadPreloadedImageHandler() {
    public void onLoad(PreloadedImage image) {
      image.setWidth("75px");
      panelImages.add(image);
    }
  };
}

4. Create your customize servlet. This is an example of how to save the received
files in a temporary folder:
public class SampleUploadServlet extends UploadAction {
  /**
   * Maintain a list with received files and their content types

   */
  Hashtable<String, File> receivedFiles = new Hashtable<String,
File>();
  Hashtable<String, String> receivedContentTypes = new
Hashtable<String, String>();

  /**
   * Override executeAction to save the received files in a
custom place
   * and delete this items from session.  
   */
  @Override
  public String executeAction(HttpServletRequest request,
List<FileItem> sessionFiles) throws UploadActionException {
    for (FileItem item : sessionFiles) {
      if (false == item.isFormField()) {
        try {
          File file = File.createTempFile("upload-", ".bin", new
File("/tmp"));
          item.write(file);
          receivedFiles.put(item.getFieldName(), file);
          receivedContentTypes.put(item.getFieldName(),
item.getContentType());
        } catch (Exception e) {
          throw new UploadActionException(e.getMessage());
        }
      }
      removeSessionFileItems(request);
    }
    return null;
  }
 
  /**
   * Remove a file when the user sends a delete request
   */
  @Override
  public void removeItem(HttpServletRequest request, String
fieldName)  throws UploadActionException {
    File file = receivedFiles.get(fieldName);
    receivedFiles.remove(fieldName);
    receivedContentTypes.remove(fieldName);
    if (file != null)  file.delete();
  }
 
  /**
   * Get the content of an uploaded file
   */
  @Override
  public void getUploadedFile(HttpServletRequest request,
HttpServletResponse response) throws IOException {
    String fieldName = request.getParameter(PARAM_SHOW);
    File f = receivedFiles.get(fieldName);
    if (f != null) {
     
response.setContentType(receivedContentTypes.get(fieldName));
      FileInputStream is = new FileInputStream(f);
      copyFromInputStreamToOutputStream(is,
response.getOutputStream());
    } else {
      renderXmlResponse(request, response,
ERROR_ITEM_NOT_FOUND);
   }
  }

You might also like