Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending File and HashMap to server in GWT

I have to send file and their properties to GWT server.

For sending a file i used form panel.

 public class BrowseFile extends DialogBox {
   // more code
   // ..

        private FormPanel getFormPanel() {
                if (formPanel == null) {
                    formPanel = new FormPanel();
                    formPanel.setMethod(FormPanel.METHOD_POST);

                    formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
                    formPanel.setAction(GWT.getHostPageBaseURL() +"UploadFileServlet");

                    formPanel.addSubmitHandler(new FormPanel.SubmitHandler(){
                        public void onSubmit(SubmitEvent event) {
                            // TODO Auto-generated method stub
                                    setFilename(fileUpload.getFilename());
                        }

                    });
                }
                return formPanel;
            }
        }   

all the properties of this file are in Hashmap GUi to add Document

there are 2 dialog box Propertybox extends DialogBox and

BrowseFile extends DialogBox

inside the constructor of PropertyBox there is BrowseFile

When PropertyBox constructor

                setSize("600px", "670px");
    setHTML("Add Document");

    setWidget(getVerticalPanel());
    browseFile = new BrowseFile();

The custom Property inside the PropertyBox depends on the class selected (Class is tree Widget)

On Server Side

public class FileUpload extends HttpServlet implements Servlet{

    private static final long serialVersionUID = 1L;
    private static final Logger log = Logger.getLogger(FileUpload.class
            .getName());
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);

    }

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

        byte[] buffer = new byte[115200];//
        String fileName = null;
        String mimetype = null;
        String majorVersion = null;
        InputStream stream = null;

        try {

            ServletFileUpload upload = new ServletFileUpload();

            FileItemIterator iterator = upload.getItemIterator(request);
            while (iterator.hasNext()) {
                FileItemStream item = iterator.next();
                 stream = item.openStream();

                if (item.isFormField()) {
        //                                                                      
                } else {

                    fileName = item.getName();
                    mimetype = item.getContentType();

//                                      
                }
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        int len;

        while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
            output.write(buffer, 0, len);
        }
        ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
        Session session =RootFolder.getSession();
        ContentStream contentStream = session.getObjectFactory()
                .createContentStream(fileName, output.size(), mimetype, input);


}

For Creating a document in External repository i need Document Property in hasmap

folder.createDocument(Document Property,contentStream, VersioningState.MAJOR);

The Document property should be send to this class on onClick event of Button ADD of Document property Class

*****How to send this document property to server in class FileUpload *****

like image 664
NewCodeLearner Avatar asked Aug 22 '12 11:08

NewCodeLearner


1 Answers

First of all, I'd recommend to use GWTUploader component, it eases life a lot.

http://code.google.com/p/gwtupload/

next, you will need add your hashmap (key/value) as form fields, see

http://code.google.com/p/gwtupload/issues/detail?id=8

and simply retrieve form fields on server-side component similar to the way you described:

            if (item.isFormField()) {
    //                                                                      
            } else {

it might look like:

            if (item.isFormField()) {
               paramsMap.add(item.getName(), item.getValue())
            } else {
like image 81
jdevelop Avatar answered Sep 28 '22 07:09

jdevelop