GWT:表单帖子仅适用于本地服务器,而不适用于应用程序引擎

时间:2013-09-14 05:39:44

标签: google-app-engine gwt servlets post

我有一个带有FormPanel,FileUpload和Button

的表单
        final FormPanel formPanel = new FormPanel();
    formPanel.setAction("uploadServlet");
    formPanel.setMethod(FormPanel.METHOD_POST);
    formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
    formPanel.setSize("100%", "100%");
    setWidget(formPanel);


    AbsolutePanel absolutePanel = new AbsolutePanel();
    formPanel.setWidget(absolutePanel);
    absolutePanel.setSize("249px", "70px");

    final FileUpload fileUpload = new FileUpload();
    fileUpload.setName("uploadFormElement");
    absolutePanel.add(fileUpload, 0, 0);

    Button btnOpen = new Button("Open");
    absolutePanel.add(btnOpen, 10, 30);

    Button btnCancel = new Button("Cancel");
    absolutePanel.add(btnCancel, 63, 30);

    this.setText("Open...");
    this.setTitle(this.getText());
    this.setAnimationEnabled(true);
    this.setGlassEnabled(true);

    btnOpen.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) { 
            formPanel.submit();
        }
    });

调用servlet但请求包含错误消息“error post”。 当我在本地服务器上试用它时,请求包含该文件,但在app引擎服务器上只有错误

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

    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<?> items = null;
    String json = null;     

    try {
        items = upload.parseRequest(request);
    }
    catch (FileUploadException e) {
        e.printStackTrace();
    }
    Iterator<?> it = items.iterator();
    while (it.hasNext()) {
        System.out.println("while (it.hasNext()) {");
        FileItem item = (FileItem) it.next();
        json = item.getString();
    }
    response.setContentType("text/html");

    ServletOutputStream out = response.getOutputStream();
    response.setContentLength(json.length());
    out.write(json.getBytes());
    out.close();
}

1 个答案:

答案 0 :(得分:0)

DiskFileItemFactory是commons-fileupload库的默认实现,基于它的javadoc:

This implementation creates FileItem instances which keep their content either in memory, for smaller items, or in a temporary file on disk, for larger items. The size threshold, above which content will be stored on disk, is configurable, as is the directory in which temporary files will be created.

If not otherwise configured, the default configuration values are as follows:

Size threshold is 10KB. Repository is the system default temp directory, as returned by System.getProperty("java.io.tmpdir").

正如您所看到的,当没有足够的内存时,此实现将在文件系统中写入。

在GAE中,存在许多约束,例如允许使用的内存,或者禁止在文件系统中写入。

您的代码在GAE开发模式下应该失败,但是您可能没有达到内存限制,或者自GAE开发人员尝试模拟与生产服务器相同的约束之后的任何限制,但它不完全相同。

说,我可以看看gwtupload库,它们有一个GAE的servlet,可以用不同的方式保存文件:BlobStore,FileApi和MemCache。