tags:

views:

52

answers:

2

Hello,

am using org.apache.commons.fileupload to upload .

public void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {

     boolean isMultipart = ServletFileUpload.isMultipartContent(request);
     GWT.log("is multipart? " + Boolean.toString(isMultipart), null);

     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() ;

                    /**
                    * Save th uploaded file
                    */

            }
     }
    catch(Exception e){
            e.printStackTrace();
    }
 }

How can i save the uploaded file ?

Thanks in Advance

A: 

This looks like server-side code, which (aside from GWT-RPC servlets) is not specific to GWT at all. That GWT.log() is unnecessary -- replace it with a regular logging call, and handle the upload as you normally would in non-GWT code, since that's what it is.

Here is a helpful example of using apache's fileupload to get you started.

Jason Hall
A: 

I think this can help you.

if (!item.isFormField()) {
    String fieldName = item.getFieldName();
    String fileName = item.getName();
    String contentType = item.getContentType();
    boolean isInMemory = item.isInMemory();
    long sizeInBytes = item.getSize();

  File saveTo = new File("/file_uploads/" + fileName);
  try {
    item.write(saveTo);
    ...
  }
  catch (Exception e){
    ...
  }

Keep in mind that uploaded file may already be automatically saved by org.apache.commons.fileupload

You can set size threshold for file to be saved on disk or loaded in memory using

// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();

// Set factory constraints
factory.setSizeThreshold(yourMaxMemorySize);
factory.setRepository(yourTempDirectory);

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Set overall request size constraint
upload.setSizeMax(yourMaxRequestSize);

// Parse the request
List /* FileItem */ items = upload.parseRequest(request);

Everything you need to know about org.apache.commons.fileupload is here: Using FileUpload

ju