0

我有一个带有 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();
}
4

1 回答 1

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。

于 2013-09-14T18:26:10.993 回答