我终于解决了我自己的问题。以下是有效方法的简要概述:
这是小程序插入代码:
String appletDivHTML = "<div> <applet id=\"uploadApplet\" " +
"code=\"<mypath>.ClientFileUpload\" " +
"archive=\"applet.jar\" width=\"100\" height=\"100\" MAYSCRIPT> </applet>" +
"<param name=\"MAYSCRIPT\" value=\"true\"/> </div>";
com.google.gwt.user.client.Element div = DOM.createDiv();
div.setInnerHTML(appletDivHTML);
BodyElement body = Document.get().getBody();
body.appendChild(div);
这是 Java 回调和 JSNI:
public static void fileCallback(String result) {
Util.showMessage("Callback reached", "Result: " + result);
}
public static native void uploadFile(String serverURL, String filePath) /*-{
$wnd.fileCallback = $entry(@<mypath>.Applet::fileCallback(Ljava/lang/String;));
var fileArray = [];
fileArray.push(filePath);
$wnd.uploadApplet.uploadFiles(serverURL, fileArray, "fileCallback");
}-*/;
调用 Java 回调的小程序代码:
JSObject window = JSObject.getWindow(this);
String[] args = new String[] {responseString};
window.call(callbackJsMethod, args);