簡體   English   中英

javafx webview javascript XMLHttpRequest responseType數組緩沖區

[英]javafx webview javascript XMLHttpRequest responseType arraybuffer

我有一個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

我找不到解決問題的方法,所以嘗試了另一種方法。 我只是借助PDFjs庫在WebView中打開pdf,然后對其進行快照。 至少我現在有文件的圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM