【问题标题】:GWT: form post works only on the local server, not with the app engineGWT:表单发布仅适用于本地服务器,不适用于应用程序引擎
【发布时间】:2013-09-18 20:56:43
【问题描述】:

我有一个带有 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”。 当我在本地服务器上尝试它时,请求包含文件,但在应用引擎服务器上只有错误

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

【问题讨论】:

  • 您能否发布您的 servlet 的重要部分。获取文件并将其内容保存在任何地方的地方。
  • 我添加了servlet代码

标签: google-app-engine gwt servlets post


【解决方案1】:

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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2019-04-03
    • 1970-01-01
    • 2012-06-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多