且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用JavaFX 2.0 WebEngine处理文件下载

更新时间:2021-07-12 23:15:32

WebView当前未实现下载功能.您可以通过监视WebView的location属性然后创建适当的代码来执行下载来自己实现它.

Download functionality is currently not implemented in WebView. You can implement it yourself by monitoring the location property of the WebView and then creating appropriate code to perform the download.

webView.getEngine().locationProperty().addListener(new ChangeListener<String>() {
  @Override public void changed(ObservableValue<? extends String> observableValue, String oldLoc, String newLoc) {
    // check if the newLoc corresponds to a file you want to be downloadable
    // and if so trigger some code and dialogs to handle the download.
  }
});

在此最新存档.

An example of code to handle a download from JavaFX can be found in this zenjava blog entry. This blog page does not exist anymore. Here is the latest archive of this blog page.

Web浏览器中的下载通常由http 内容类型href ="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html"触发>或内容处置标头,并且可以基于mime类型/文件扩展名映射.以上方案仅适用于文件扩展名映射,其中文件扩展名是从该位置派生的.为了根据内容类型或内容处置标头处理下载,您可能需要实现自己的java.net url连接处理程序.

Downloads in web browsers are often triggered by http content-type or content-disposition headers and can be based upon a mime-type/file extension mapping. The above scheme will only work for a file extension mapping where the file extension is derived from the location. For handling downloads based on a content-type or content-disposition header you would likely need to implement your own java.net url connection handler.

要在核心JavaFX库中实现此功能,可以检查 JavaFX Jira 的功能围绕此请求,如果不存在,则创建一个新的功能请求.

To get this functionality implemented in the core JavaFX libraries you could check the JavaFX Jira for a feature request around this and, if it is not there, create a new feature request.