Java 为媒体创建URI

Java 为媒体创建URI,java,javafx,uri,media,Java,Javafx,Uri,Media,我正在用Java创建一个mp3播放器。 我想为媒体创建一个URI。 仅当文件路径中没有空格时,它才起作用,但如果有空格,它将抛出URISyntaxException 我的类中有一个方法,该方法将返回一个字符串,该字符串表示我为媒体创建的URI。它就是不起作用 private static String convertToFileURL ( String filename ) { String path = new File ( filename ).getAbsolutePath ();

我正在用Java创建一个mp3播放器。 我想为媒体创建一个URI。 仅当文件路径中没有空格时,它才起作用,但如果有空格,它将抛出URISyntaxException

我的类中有一个方法,该方法将返回一个字符串,该字符串表示我为媒体创建的URI。它就是不起作用

private static String convertToFileURL ( String filename )
{
    String path = new File ( filename ).getAbsolutePath ();
    if ( File.separatorChar != '/' )
    {
        path = path.replace ( File.separatorChar, '/' );
    }
    if ( !path.startsWith ( "/" ) )
    {
        path = "/" + path;
    }
    URI uri = URI.create(path).normalize();



    String retVal =  "file://" + uri.toString();

    return retVal;
}
有没有更简单的方法为RFC2396标准创建URI?

有吗

File file = new File(filename);
URI uri = file.toURI();
String retVal = uri.toString();
你需要什么