Servlets GWT文件上载表单提交POST请求无法读取文件
我尝试了文件上传,参考了GWT源文件。因为我想把它添加到不同的选项卡上,所以我为它创建了GWT页面,并在那里添加了FileUpload。 未实现入口点,因为其已在其根页面中实现。 我并没有使用onModuleLoad方法,我只是创建方法来显示元素并将其添加到FormPanel中 我能够提交POST请求,但无法在servlet上捕获文件。我在GWT端或servlet端做错了什么吗 我在GWT端使用了类似的代码Servlets GWT文件上载表单提交POST请求无法读取文件,servlets,gwt,servlet-3.0,gwt2,Servlets,Gwt,Servlet 3.0,Gwt2,我尝试了文件上传,参考了GWT源文件。因为我想把它添加到不同的选项卡上,所以我为它创建了GWT页面,并在那里添加了FileUpload。 未实现入口点,因为其已在其根页面中实现。 我并没有使用onModuleLoad方法,我只是创建方法来显示元素并将其添加到FormPanel中 我能够提交POST请求,但无法在servlet上捕获文件。我在GWT端或servlet端做错了什么吗 我在GWT端使用了类似的代码 public class FormPanelExample implements Com
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();
if(!ServletFileUpload.isMultipartContent(request)){
抛出新的FileUploadException(“未找到错误多部分请求”);
}
DiskFileItemFactory=新的DiskFileItemFactory();
ServletFileUpload upload=新的ServletFileUpload(工厂);
列表项=upload.parseRequest(请求);
如果(items==null){
response.getWriter().write(“文件未正确上载”);
返回;
}
迭代器iter=items.Iterator();
当我调用iter.next()时,它给出的错误是没有这样的elementFound异常
异常情况下,它似乎处于提交状态。文件未提交到servlet请求。尝试使用邮递员调用端点,并将文件直接上载到正在运行的servlet,以确保其正常工作 我检查了我自己的代码实现,它几乎与您的完全匹配,除了面板上的FileUpload之外,我没有使用任何东西。删除文本框和列表框,这样我们就可以检查文件部分是否独立工作,然后介绍每个项目并分别测试 我发现这在服务器端更可靠
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
FileItemIterator iter = servletFileUpload.getItemIterator(request);