为媒体创建URI

时间:2015-05-16 09:38:29

标签: java javafx uri media

我用Java创建一个mp3播放器。 我想为Media创建一个URI。 仅当文件路径中没有空格时才有效,但如果有空格,则抛出URISyntaxException。

我的类中有一个方法会返回一个String,表示我为Media创建的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;
}

是否有更简单的方法为RFC 2396标准创建URI?

1 个答案:

答案 0 :(得分:0)

确实

File file = new File(filename);
URI uri = file.toURI();
String retVal = uri.toString();

做你需要的吗?