我无法使用GWT 2.7.0上传excel文件和解析。提到了许多链接Link1 Link2
使用上述技术和RequestBuilder我无法将解析后的Excel数据发送回客户端。最后实现了GWT RPC技术,但由于GWT无法在客户端上实现File.io api(Javascript或浏览器无法读取),因此加载Excel文件时出现问题
代码:
客户端FileUploading
public class MyFileUpload extends Composite implements Constants{
private ExcelClientServiceImpl excelServiceClient;
private VerticalPanel vPanel;
public MyFileUpload(ExcelClientServiceImpl excelServiceClient){
this.excelServiceClient = excelServiceClient;
this.vPanel = new VerticalPanel();
initWidget(this.vPanel);
}
public void initiateUpload() {
// Create a FormPanel and point it at a service.
final FormPanel form = new FormPanel();
form.setAction("/excelParser");
// 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.
vPanel = new VerticalPanel();
form.setWidget(vPanel);
// Create a FileUpload widget.
final FileUpload upload = new FileUpload();
//upload.setName("uploadFormElement");
vPanel.add(upload);
System.out.println("File name is : "+upload.getFilename());
// Add a 'submit' button.
vPanel.add(new Button("Submit", new ClickHandler() {
public void onClick(ClickEvent event) {
Window.alert("In Button >>>>>> "+event);
form.submit();
}
}));
// Add an event handler to the form.
form.addSubmitHandler(new FormPanel.SubmitHandler() {
public void onSubmit(SubmitEvent event) {
Window.alert("In Handler >>>>>> "+event);
}
});
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("In complete >>>>>> "+event.getResults());
}
});
RootPanel.get().add(form);
}
}
客户端界面
public interface ExcelClientServiceInt {
void parse(String fileName, String[] lines, StringBuilder integerNumber, StringBuilder floatNumber,
String separator);
void readingExcel();
}
客户端实施
public class ExcelClientServiceImpl implements ExcelClientServiceInt{
private ExcelServiceIntAsync service;
private MyFileUpload excelUpload;
public ExcelClientServiceImpl(String url){
this.service = GWT.create(ExcelParserService.class);
ServiceDefTarget endpoint = (ServiceDefTarget) this.service;
endpoint.setServiceEntryPoint(url);
this.excelUpload = new MyFileUpload(this);
}
@Override
public void parse(String fileName, String[] lines, StringBuilder integerNumber, StringBuilder floatNumber,
String separator) {
this.service.parse(fileName, lines, integerNumber, floatNumber, separator, new DefaultCallback());
}
@Override
public void readingExcel() {
this.service.readingExcel(null, new DefaultCallback());
}
private class DefaultCallback implements AsyncCallback{
@Override
public void onFailure(Throwable caught) {
System.out.println("Output failed");
}
@Override
public void onSuccess(Object result) {
System.out.println("Output reieved successfully "+result);
}
}
}
服务界面
@RemoteServiceRelativePath("excelParser")
public interface ExcelServiceInt extends RemoteService { void parse(String fileName,String [] lines,StringBuilder integerNumber,StringBuilder floatNumber,String separator); public List readingExcel(String fileName); }
异步回拨
public interface ExcelServiceIntAsync{
void parse(String fileName, String[] lines, StringBuilder integerNumber, StringBuilder floatNumber,
String separator, AsyncCallback<Void> callBack);
void readingExcel(String fileName, AsyncCallback<List<String>> callBack);
}
服务器端服务
public class ExcelParserService extends RemoteServiceServlet implements ExcelServiceInt{
public void parse(String fileName, String[] lines, StringBuilder integerNumber, StringBuilder floatNumber,
String separator) {
ExcelParser.parse(fileName, lines, integerNumber, floatNumber, separator);
}
public List<String> readingExcel(String fileName) {
return ExcelParser.readingExcel(fileName);
}
}