8

我必须将文件及其属性发送到 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;
            }
        }   

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

有2个对话框Propertybox extends DialogBox

BrowseFile extends DialogBox

在 PropertyBox 的构造函数里面有 BrowseFile

当 PropertyBox 构造函数

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

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

PropertyBox里面的自定义Property依赖于选择的类(Class是tree 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);


}

为了在外部存储库中创建文档,我需要 hasmap 中的文档属性

folder.createDocument(文档属性,contentStream,VersioningState.MAJOR);

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

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

4

2 回答 2

8
于 2012-08-24T12:16:54.977 回答
1

为什么不使用一系列请求?

  1. 客户:上传文件。使用表单面板。
    服务器:保存文件并返回新的fileId(如果成功)

  2. 客户端:(来自 onSubmitCompleted)将文件的数据连同其 fileId 一起发送。使用您需要的任何数据结构
    服务器:将文件信息字段存储到数据库中。

于 2012-08-24T21:30:55.420 回答