我创建了小部件,并直接添加到根面板
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,但它没有找到任何文件
谁能解释我为什么会有如此不同的行为,以及我该如何解决?