客户端:
class FileUploadForm extends FormPanel{
private FileUpload fileUpload;
public FileUploadForm(){
setEncoding(FormPanel.ENCODING_MULTIPART);
setMethod(FormPanel.METHOD_POST);
VerticalPanel holder = new VerticalPanel();
fileUpload = new FileUpload();
fileUpload.setName("file");
fileUpload.setWidth(300);
holder.add(fileUpload);
}
public String getFileName(){
return fileUpload.getFilename();
}
}
上面的代码是构造一个简单的文件上传的Panel。
使用FileUploadForm:
(1):设置Action:fileUploadForm.setAction(String url);//
(2):上传前的事件:fileUploadForm.addSubmitHandler(SubmitHandler handler);//
(3):上传完成事件:fileUploadForm.addSubmitCompleteHandler(SubmitCompleteHandler handler);//
如果服务器端用诸如out.print写入数据返回到客户端,在SubmitCompleteHandler里可以用如下方式获得。
//获取输出流里的数据
String result = event.getResults();
HTML html = new HTML(result);
result = html.getText();
(4):执行上传操作:fileUploadForm.submit();
服务端:
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter= upload.getItemIterator(req);
while(iter.hasNext()){
FileItemStream item = iter.next();
InputStream in = item.openStream();//打开上传的文件流
}