我使用UI.xml创建了fileupload小部件。书面Servlet。我的servlet正在调用但没有文件内容正在检索。我在这里粘贴了我的代码。你能否回答一下问题是什么以及如何检索内容?这是第一次使用它。请告诉我。
UI
<g:FormPanel ui:field="uploadDPAFormPanel">
<g:HorizontalPanel>
<g:Label> File
Upload: </g:Label>
<g:FileUpload ui:field="fileUpload" />
<g:Button ui:field="uploadButton" title="Upload"
Upload</g:Button>
</g:HorizontalPanel>
</g:FormPanel>
以下是我的实施课程:
@UiHandler("uploadButton")
void onClickUploadButton(ClickEvent event) {
GWT.log("You selected: " + fileUpload.getFilename(), null);
uploadDPAFormPanel.submit();
}
public void init() {
uploadDPAFormPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
uploadDPAFormPanel.setMethod(FormPanel.METHOD_POST);
uploadDPAFormPanel.setAction(GWT.getHostPageBaseURL()
+ uploadServlet);
uploadDPAFormPanel.addSubmitHandler(new FormPanel.SubmitHandler() {
@Override
public void onSubmit(SubmitEvent event) {
if (!"".equalsIgnoreCase(fileUpload.getFilename())) {
GWT.log("UPLOADING FILE????", null);
// NOW WHAT????
}
else{
GWT.log("UPLOA event cancel");
event.cancel(); // cancel the event
}
}
});
uploadDPAFormPanel
.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
//TODO need to write code to refresh the page
doAlert("Uploaded sucessfully");
}
});
}
以下是我的servlet类:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletFileUpload upload = new ServletFileUpload();
log.debug("UploadServlet upload:"+upload);
try {
FileItemIterator iter = upload.getItemIterator(request);
log.debug("UploadServlet iter:"+iter);
System.out.println("UploadServlet iter:"+iter);
while (iter.hasNext()) {
FileItemStream item = iter.next();
log.debug("UploadServlet iter:Field Name:"+item.getFieldName());
System.out.println(":Field Name:"+item.getFieldName());
System.out.println(":Name:"+item.getName());
System.out.println(":is form field:"+item.isFormField());
String name = item.getFieldName();
InputStream stream = item.openStream();
// Process the input stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[8192];
while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
int maxFileSize = 10 * (1024 * 2); // 10 megs max
if (out.size() > maxFileSize) {
throw new RuntimeException("File is > than " + maxFileSize);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
答案 0 :(得分:1)
您应在name="..."
上设置FileUpload
。