在GWT中将文件和HashMap发送到服务器

New*_*ner 8 java gwt servlets gwt-rpc gwt2

我必须将文件及其属性发送到GWT服务器.

用于发送文件我使用表单面板.

 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;
            }
        }   
Run Code Online (Sandbox Code Playgroud)

该文件的所有属性都在Hashmap中 GUi添加文档

有2个对话框Propertybox extends DialogBox

BrowseFile extends DialogBox
Run Code Online (Sandbox Code Playgroud)

在PropertyBox的构造函数中有BrowseFile

当PropertyBox构造函数

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

    setWidget(getVerticalPanel());
    browseFile = new BrowseFile();
Run Code Online (Sandbox Code Playgroud)

PropertyBox内的自定义属性取决于所选的类(Class是树Widget)

在服务器端

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


}
Run Code Online (Sandbox Code Playgroud)

对于在外部存储库中创建文档,我需要在hasmap中使用Document Property

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

应该在Document属性Class的onClick事件上将Document属性发送到此类Button ADD

*****如何在FileUpload类*****中将此文档属性发送到服务器

jde*_*lop 8

首先,我建议使用GWTUploader组件,它可以简化生活.

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

接下来,您需要将您的hashmap(键/值)添加为表单字段,请参阅

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

并简单地检索服务器端组件上的表单字段,类似于您描述的方式:

            if (item.isFormField()) {
    //                                                                      
            } else {
Run Code Online (Sandbox Code Playgroud)

它可能看起来像:

            if (item.isFormField()) {
               paramsMap.add(item.getName(), item.getValue())
            } else {
Run Code Online (Sandbox Code Playgroud)