4

我找到了一种使用 HostServices 在默认浏览器上打开链接的方法。

getHostServices().showDocument("http://www.google.com");
  • 有没有办法在默认媒体播放器中打开媒体?
  • 有什么方法可以启动特定的文件应用程序
4

3 回答 3

7

一般来说,您可以使用以下方式本地Desktop#open(file)打开文件:

final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.OPEN)) {
    desktop.open(file);
} else {
    throw new UnsupportedOperationException("Open action not supported");
}

启动关联的应用程序以打开文件。如果指定的文件是目录,则启动当前平台的文件管理器打开它。

更具体地说,如果是浏览器,您可以直接使用Desktop#browse(uri),如下所示:

final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
    desktop.browse(uri);
} else {
    throw new UnsupportedOperationException("Browse action not supported");
}

启动默认浏览器以显示URI. 如果默认浏览器不能处理指定的 ,则调用为处理指定类型而URI注册的应用程序。应用程序由类定义 URIs的协议和路径确定。如果调用线程没有必要的权限,并且这是从小程序内调用的, 则使用。类似地,如果调用没有必要的权限,并且这是从 Java Web Started 应用程序中调用的,则使用。URIURIAppletContext.showDocument()BasicService.showDocument()

于 2016-10-06T14:41:19.657 回答
5

如果您想http:在浏览器中打开具有方案的 URL,或者使用该文件类型的默认应用程序打开文件,HostServices.showDocument(...)您引用的方法提供了一种“纯 JavaFX”方式来执行此操作。请注意,您不能使用它(据我所知)从 Web 服务器下载文件并使用默认应用程序打开它。

要使用默认应用程序打开文件,您必须将文件转换为file:URL 的字符串表示形式。这是一个简单的例子:

import java.io.File;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class OpenResourceNatively extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField textField = new TextField("http://stackoverflow.com/questions/39898704");
        Button openURLButton = new Button("Open URL");
        EventHandler<ActionEvent> handler = e -> open(textField.getText());
        textField.setOnAction(handler);
        openURLButton.setOnAction(handler);

        FileChooser fileChooser = new FileChooser();
        Button openFileButton = new Button("Open File...");
        openFileButton.setOnAction(e -> {
            File file = fileChooser.showOpenDialog(primaryStage);
            if (file != null) {
                open(file.toURI().toString());
            }
        });

        VBox root = new VBox(5, 
                new HBox(new Label("URL:"), textField, openURLButton),
                new HBox(openFileButton)
        );

        root.setPadding(new Insets(20));
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    private void open(String resource) {
        getHostServices().showDocument(resource);
    }

    public static void main(String[] args) {
        launch(args);
    }
}
于 2016-10-06T17:59:27.977 回答
4

只有解决方案才能java.awt.Desktop让我从 JavaFX 打开文件。

然而,起初,我的应用程序卡住了,我不得不弄清楚有必要Desktop#open(File file)从新线程调用。从当前线程或 JavaFX 应用程序线程调用该方法会Platform#runLater(Runnable runnable)导致应用程序无限期挂起,而不会引发异常。

这是一个带有工作文件打开解决方案的小型 JavaFX 应用程序示例:

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class FileOpenDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        final Button button = new Button("Open file");

        button.setOnAction(event -> {
            final FileChooser fileChooser = new FileChooser();
            final File file = fileChooser.showOpenDialog(primaryStage.getOwner());

            if (file == null)
                return;

            System.out.println("File selected: " + file.getName());

            if (!Desktop.isDesktopSupported()) {
                System.out.println("Desktop not supported");
                return;
            }

            if (!Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
                System.out.println("File opening not supported");
                return;
            }

            final Task<Void> task = new Task<Void>() {
                @Override
                public Void call() throws Exception {
                    try {
                        Desktop.getDesktop().open(file);
                    } catch (IOException e) {
                        System.err.println(e.toString());
                    }
                    return null;
                }
            };

            final Thread thread = new Thread(task);
            thread.setDaemon(true);
            thread.start();
        });

        primaryStage.setScene(new Scene(button));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

另一个建议的解决方案javafx.application.HostServices根本不起作用。我在 Ubuntu 17.10 amd64 上使用 OpenJFX 8u141,调用时出现以下异常HostServices#showDocument(String uri)

java.lang.ClassNotFoundException: com.sun.deploy.uitoolkit.impl.fx.HostServicesFactory

显然,JavaFX HostServices 尚未在所有平台上正确实现。关于这个主题,另见:https ://github.com/Qabel/qabel-desktop/issues/420

于 2017-12-27T19:10:16.087 回答