1

我想将文件从客户端发送到服务器。

我的代码:

客户端

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.setWidget(getFlexTable_1());

                System.out.println(GWT.getHostPageBaseURL() +"UploadFileServlet");
    }
    return formPanel;
}

getFlexTable_1()

flexTable.setWidget(1, 1, getFileUpload());

getFileUpload()

private FileUpload getFileUpload() {
    if (fileUpload == null) {
        fileUpload = new FileUpload();
        fileUpload.setName("upload");
    }
    return fileUpload;
}

private Button getAddButton() {
        if (addButton == null) {
            addButton = new Button("ADD");
            addButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                               formPanel.submit();
        }
        });
    }
return addButton;

}

在服务器端

public class CmisFileUpload extends HttpServlet implements Servlet{

    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;
        System.out.println("ServletWorking Fine");
}

现在,当我选择一个文件并单击添加按钮时,我无法在服务器端看到此代码的输出System.out.println("ServletWorking Fine");

System.out.println(GWT.getHostPageBaseURL() +"UploadFileServlet");客户端的输出是

http://127.0.0.1:8888/UploadFileServlet

当我直接在浏览器上使用这个 url 时,我得到了服务器端的输出System.out.println("ServletWorking Fine");**


已编辑

我又创建了一个用于文件上传的 Web 应用程序

public class Uploadfile implements EntryPoint {

    FormPanel uploadForm = new FormPanel();
    public void onModuleLoad() {

        HorizontalPanel horizontalPanel = new HorizontalPanel();

      uploadForm.setAction(GWT.getHostPageBaseURL() +"UploadFileServlet"); 

        uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART); 
        uploadForm.setMethod(FormPanel.METHOD_POST); 
        horizontalPanel.add(uploadForm);

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

        FlexTable flexTable = new FlexTable();
        panel.add(flexTable);

        // Create a FileUpload widget. 
        FileUpload upload = new FileUpload(); 
        upload.setName("uploadFormElement"); 
        flexTable.setWidget(2, 3, upload);
        // panel.add(upload); 

        // Add a 'submit' button. 
        Button uploadSubmitButton = new Button("Submit"); 
        panel.add(uploadSubmitButton); 

        uploadSubmitButton.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                // TODO Auto-generated method stub
                 uploadForm.submit(); 
            }
        });
        uploadForm.addFormHandler(new FormHandler() { 
          public void onSubmit(FormSubmitEvent event) { 
          } 
          public void onSubmitComplete(FormSubmitCompleteEvent event) { 
            Window.alert(event.getResults()); 
          } 
        }); 
        RootPanel.get().add(horizontalPanel); 
    }
}

服务器

protected void doGet(HttpServletRequest request, 
            HttpServletResponse response) 
                            throws ServletException, IOException { 
      doPost(request, response);
  }

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

      System.out.println("working fine" );
   }

此代码工作正常

在我看来,代码之间没有区别。

请告诉我为什么 formpanel.submit 不能正常工作。

请帮忙。

4

2 回答 2

5

方法是hide()关闭窗口???如果是,那么

hide();之后删除移动代码formPanel.submit();

hide()使用FormHandler。例如

uploadForm.addFormHandler(new FormHandler() {

    public void onSubmitComplete(FormSubmitCompleteEvent event) {
        hide();
    }

    public void onSubmit(FormSubmitEvent event) {

    }

});

原因:FormPanel不能分离(即从其父级中移除,直到提交完成。否则,提交通知将失败。

于 2012-08-23T15:56:48.457 回答
0

为什么您为文件上传映射了 GET 方法。GET 请求方法为在浏览器中输入的 url 服务。删除 GET 请求映射,它将起作用。

对于 POST 请求映射,您可以将 MultipartFile 用于 RequestParam,如下所示

protected void uploadFileAndReconcilePayout1(@RequestParam("documentUpload") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //code for file working

    }
于 2012-08-23T05:30:54.540 回答