0

我创建了小部件,并直接添加到根面板

public class FileUploadWidget extends VerticalPanel {
    public FileUploadWidget() {
        super();
        final FormPanel form = new FormPanel();

        form.setMethod(FormPanel.METHOD_POST);
        form.setEncoding(FormPanel.ENCODING_MULTIPART);
        form.setAction("/FileUpload"); 

        form.setWidget(this);

        final FileUpload fileUpload = new FileUpload();

        fileUpload.setName("uploadFormElement");  
        this.add(fileUpload);

        Label maxUpload = new Label();
        maxUpload.setText("Maximum upload file size: 1MB");
        this.add(maxUpload);

        Button button = new Button("Submit");
        button.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                form.submit();

            }
        });
        this.add(button);

        form.addSubmitCompleteHandler(new SubmitCompleteHandler() {

            public void onSubmitComplete(SubmitCompleteEvent event) {
                Window.alert(event.getResults());

            }
        });
    }

}

我创建了 servlet

public class FileUploadServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

      protected void doPost(HttpServletRequest request, 
    HttpServletResponse response) 
          throws ServletException, IOException { 
        response.setContentType("text/plain"); 

        FileItem uploadItem = getFileItem(request); //BREAKPOINT

        if (uploadItem == null) { 
          response.getWriter().write("NO-SCRIPT-DATA"); 
          return; 
        } 
        try {
            uploadItem.write(new File("newfile.txt"));
        } catch (Exception e) {
            response.getWriter().write("ERROR");
        }

        response.getWriter().write("OK"); 
      } 

      private FileItem getFileItem(HttpServletRequest request) { 
        FileItemFactory factory = new DiskFileItemFactory(); 
        ServletFileUpload upload = new ServletFileUpload(factory); 

        try { 
          List items = upload.parseRequest(request); 
          Iterator it = items.iterator(); 
          while (it.hasNext()) { 
            FileItem item = (FileItem) it.next(); 
            if (!item.isFormField() 
                && "uploadFormElement".equals(item.getFieldName())) { 
              return item; 
            } 
          } 
        } catch (FileUploadException e) { 
          return null; 
        } 
        return null; 
      } 
     }

我把我的 web.xml 文件映射到 servlet

<servlet>
    <servlet-name>FileUpload</servlet-name>
    <servlet-class>my.web.app.GwtFileUpload.server.FileUploadServlet</servlet-class>
</servlet>
 ....
<servlet-mapping>
    <servlet-name>FileUpload</servlet-name>
    <url-pattern>/FileUpload</url-pattern>
</servlet-mapping>

现在,当我选择文件并调用提交时,当我使用 firefox 时,什么都没发生(我得到的只是 SubmitEvent),我在 servlet 代码中标记的断点永远不会到达

当我在 chrome 下尝试相同的代码时,它正在进入 servlet,但它没有找到任何文件

谁能解释我为什么会有如此不同的行为,以及我该如何解决?

4

3 回答 3

3

FileUpload部件包装HTML 元素。FormPanel如果要将其提交到服务器,则必须使用此小部件。因此有必要将 fileupload 添加到FormPanel. 您已将其直接添加到Panel. 请替换代码

this.add(fileUpload);

form.add(fileUpload);

有关更多详细信息,请浏览链接http://www.tutorialspoint.com/gwt/gwt_fileupload_widget.htm

于 2013-06-12T17:38:42.313 回答
2

将 fileUpload 控件添加到FormPanel中,因为FileUpload widget(那些实现 HasName 的)如果包含在此面板中,将被提交到服务器。

完成后,您将被添加fileupload到 verticlepanel 中form.setWidget(this);。喜欢

this.add(fileUpload);
form.setWidget(this);
于 2013-06-12T16:38:04.927 回答
0

我建议使用 gwtupload 库,它将节省大量编写您自己的代码的时间,并为您的应用程序提供许多功能,如自定义按钮、进度条、文件扩展名、大小限制等。

你有这个GettingStarted和这个示例页面。

于 2013-06-13T05:56:23.107 回答