GWT文件上传表单提交POST请求无法读取文件

时间:2018-07-07 19:00:15

标签: servlets gwt servlet-3.0 gwt2

我尝试了FileUpload参考GWT源文档。由于我想将其添加到其他选项卡上,因此我为此创建了GWT页面,并在该页面上添加了FileUpload。 由于在其根页面中已实现,因此不实施entryPoint。 我没有使用onModuleLoad方法,我只是创建方法来显示元素并将其添加到FormPanel。

我能够提交POST请求,但无法在servlet上捕获文件。我在GWT端或servlet端做错了吗?

我在GWT端使用了类似的代码

public class FormPanelExample implements Composite {

  public void FormPanelExample() {
    // Create a FormPanel and point it at a service.
    final FormPanel form = new FormPanel();
    form.setAction("/myFormHandler");

    // Because we're going to add a FileUpload widget, we'll need to set the
    // form to use the POST method, and multipart MIME encoding.
    form.setEncoding(FormPanel.ENCODING_MULTIPART);
    form.setMethod(FormPanel.METHOD_POST);

    // Create a panel to hold all of the form widgets.
    VerticalPanel panel = new VerticalPanel();
    form.setWidget(panel);

    // Create a TextBox, giving it a name so that it will be submitted.
    final TextBox tb = new TextBox();
    tb.setName("textBoxFormElement");
    panel.add(tb);

    // Create a ListBox, giving it a name and some values to be associated with
    // its options.
    ListBox lb = new ListBox();
    lb.setName("listBoxFormElement");
    lb.addItem("foo", "fooValue");
    lb.addItem("bar", "barValue");
    lb.addItem("baz", "bazValue");
    panel.add(lb);

    // Create a FileUpload widget.
    FileUpload upload = new FileUpload();
    upload.setName("uploadFormElement");
    panel.add(upload);

    // Add a 'submit' button.
    panel.add(new Button("Submit", new ClickHandler() {
      public void onClick(ClickEvent event) {
        form.submit();
      }
    }));

    // Add an event handler to the form.
    form.addSubmitHandler(new FormPanel.SubmitHandler() {
      public void onSubmit(SubmitEvent event) {
        // This event is fired just before the form is submitted. We can take
        // this opportunity to perform validation.
        if (tb.getText().length() == 0) {
          Window.alert("The text box must not be empty");
          event.cancel();
        }
      }
    });
    form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
      public void onSubmitComplete(SubmitCompleteEvent event) {
        // When the form submission is successfully completed, this event is
        // fired. Assuming the service returned a response of type text/html,
        // we can get the result text here (see the FormPanel documentation for
        // further explanation).
        Window.alert(event.getResults());
      }
    });

    RootPanel.get().add(form);
  }
}

在Servlet端

if (!ServletFileUpload.isMultipartContent(request)) {                                 
            throw new FileUploadException("error multipart request not found");              
        }       

        DiskFileItemFactory factory = new DiskFileItemFactory();            

        ServletFileUpload upload = new ServletFileUpload(factory);

        List<FileItem> items = upload.parseRequest(request);

        if (items == null) {            
            response.getWriter().write("File not correctly uploaded");
            return;
        }

        Iterator<FileItem> iter = items.iterator();

当我调用iter.next()时,它给出错误,没有此类elementFound异常 例外地,它似乎在提交文件上没有提交到servlet请求。

1 个答案:

答案 0 :(得分:0)

尝试使用邮递员调用端点并将文件直接上传到正在运行的Servlet,以确保其正常工作。

我检查了我自己的代码实现,它几乎与您的代码完全匹配,除了我除了面板上的FileUpload以外没有使用其他任何东西。删除TextBox和ListBox,以便我们可以检查文件部分是否单独工作,然后介绍每个项目并分别进行测试。

我发现这在服务器端更可靠

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
FileItemIterator iter = servletFileUpload.getItemIterator(request);