我有一个javafx应用程序,我在webview中打开一个网站并执行一个javascript函数。在这个js函数中,我发出XMLHttpRequest请求来获取pdf文件,我试图将它发送回java。问题在于编码,因为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
答案 0 :(得分:0)
我找不到任何问题的解决方案,所以我尝试了另一种方式。我只是在PDFjs库的帮助下打开WebView中的pdf,然后我拍摄它的快照。至少我现在有一个文件的图像。