Tuesday, September 24, 2013

Re: Storing uploaded file in gwt (server side)

You can do it through a servlet. Below is an example of the servlet code. Do not forget to declare the servlet in web.xml.


import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


public class UploadServlet extends HttpServlet {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
   
    /**
     * Accepts a Http request parameters and post a file to UforgeBuilder end
     * point. Used to upload profile picture.
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        try {

            // Check that we have a file upload request
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            if (isMultipart) {

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

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

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

                // Process the uploaded items
                Iterator<?> iter = items.iterator();

                File uploadedFile = null;

                while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();
                    /* text field */
                    // here you can retrieve text fields
                    if (item.isFormField()) {
                        if (item.getFieldName().equals("xxxx")) String field1 = item.getString();
                        else if (item.getFieldName().equals("xxxx")) String field2 = item.getString();
                       
                    }
                    /* upload file field */
                    else {
                        if (!item.getName().isEmpty()) {
                            String fileName = item.getName();
                            String folder=this.getServletContext().getRealPath(Config.UPLOAD_DIRECTORY);
                            File uploadDirectory = new File(folder);
                            if (!uploadDirectory.exists()) uploadDirectory.mkdirs();
                            uploadedFile = new File(uploadDirectory, fileName);
                            item.write(uploadedFile);
                        }
                    }

                    // now you can handle the file "uploadedFile"
                }           
            }

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

    }
}

-----------------------------------------------------------------------------------------------
Le lundi 16 septembre 2013 20:07:07 UTC+1, Pradeep Kumar a écrit :








 

Hi All,  

I am new to GWT, I am facing some issue with file creation.

I am uploading a file from client side and want to store that file in server side and parse it and create tables in database for the data present in the file.

When I try to create a file at server side there is some file.io permission issue App engine is not allowing me to create the file.

Please let me know how can I solve this issue.

I did browse net, didn't find any solutions.

In the mean time I did read about sandboxing and some data store concept

Please let me know how can I use for my problem.

Thanks in Advance, Pradeep

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment