【发布时间】:2018-04-29 20:45:09
【问题描述】:
我有一个 javafx 应用程序,我在 webview 中打开一个网站并执行一个 javascript 函数。在这个 js 函数中,我发出 XMLHttpRequest 请求以获取 pdf 文件,并且我试图将其发送回 java.util.xml。问题在于编码,因为 pdf 是二进制的,并且响应被转换为 UTF-8 文本。我现在的功能是这样的:
webEngine.executeScript(
"function xhrDownload() {"
+ "var req = new XMLHttpRequest();"
+ "req.onload = function () {"
+ "if (req.readyState === req.DONE && req.status == 200) {"
+ "java.sendData(req.responseText);"
+ "}"
+ "};"
+ "req.open(\"GET\", \"https://xxxx/test.pdf\", false);"
+ "req.send();"
+ "}xhrDownload();"
);
我尝试设置responseType ='arraybuffer',但出现此错误:
Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: InvalidAccessError (DOM Exception 15): The object does not support the operation or argument.
at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:156)
at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
at com.sun.webkit.WebPage.executeScript(WebPage.java:1509)
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:1005)
at main.xhrDownload(main.java:77)
at main.lambda$0(main.java:54)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.web.WebEngine$LoadWorker.updateState(WebEngine.java:1287)
at javafx.scene.web.WebEngine$LoadWorker.dispatchLoadEvent(WebEngine.java:1398)
at javafx.scene.web.WebEngine$LoadWorker.access$1200(WebEngine.java:1280)
at javafx.scene.web.WebEngine$PageLoadListener.dispatchLoadEvent(WebEngine.java:1267)
at com.sun.webkit.WebPage.fireLoadEvent(WebPage.java:2499)
at com.sun.webkit.WebPage.fwkFireLoadEvent(WebPage.java:2343)
at com.sun.webkit.network.URLLoader.twkDidFinishLoading(Native Method)
at com.sun.webkit.network.URLLoader.notifyDidFinishLoading(URLLoader.java:859)
at com.sun.webkit.network.URLLoader.lambda$didFinishLoading$103(URLLoader.java:850)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
此应用程序的主要目标是下载此 pdf 文件。但我不能只从 java 向文件 url 发出请求,因为这个文件只有在你登录到网站后才可用。(会话保护或类似的东西)。
提前感谢您的帮助:D
【问题讨论】:
标签: javascript java webview xmlhttprequest