GWT文件上传表单提交POST请求无法读取文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GWT文件上传表单提交POST请求无法读取文件相关的知识,希望对你有一定的参考价值。
我试过FileUpload引用GWT源文档。由于我想在不同的选项卡上添加它,我已经为此创建了GWT页面,并在那里添加了FileUpload。自从在其根页面中实现后,它没有实现。我没有使用onModuleLoad方法我只是创建显示元素的方法并将其添加到FormPanel。
我能够提交POST请求但无法捕获servlet上的File。我在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请求。
答案
尝试使用邮递员调用端点并将文件直接上传到正在运行的Servlet,以确保其正常工作。
我检查了我自己的这段代码的实现,它几乎与你的完全匹配,除了我没有使用除面板上的FileUpload之外的任何东西。删除TextBox和ListBox,以便我们可以检查文件部分是否单独工作,然后引入每个项目并单独测试它。
我发现这在服务器端更可靠
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
FileItemIterator iter = servletFileUpload.getItemIterator(request);
以上是关于GWT文件上传表单提交POST请求无法读取文件的主要内容,如果未能解决你的问题,请参考以下文章