如何解决java.net.URISyntaxException错误

时间:2018-03-08 14:45:19

标签: java file url path uri

我想获取音乐文件的路径,而不是使用以下代码在我的MP3播放器中添加该文件:

addSong.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (fileChooser.showSaveDialog(MainFrame.this) == JFileChooser.APPROVE_OPTION) {
                File song = fileChooser.getSelectedFile();

                mainPanel.addSongs(song.getAbsolutePath()); // getting file path

                JOptionPane.showMessageDialog(MainFrame.this,
                        "Song has been successfully added to your music track!", "Information",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        }
    });

比我传这样:

public void addSongs(String path) {
    musicPlayer.addSongs(path);
}

最后来到这里:

public void addSongs(String path) {

    Media media = new Media(path); // this is place where error occurs

    MediaPlayer mediaPlayer = new MediaPlayer(media);

    songList.add(mediaPlayer);

}

我收到了一个错误:

Exception in thread "AWT-EventQueue-0"        java.lang.IllegalArgumentException: java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Users\Jovan\Desktop\DEAF KEV - Invincible [NCS Release].mp3
at javafx.scene.media.Media.<init>(Media.java:385)
at mp3.MusicPlayer.addSongs(MusicPlayer.java:86)
at mp3.MainPanel.addSongs(MainPanel.java:40)
at mp3.MainFrame$2.actionPerformed(MainFrame.java:78)

如何解决这个问题?

0 个答案:

没有答案