【发布时间】:2015-05-16 09:38:28
【问题描述】:
我正在用 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;
}
有没有更简单的方法来为 RFC 2396 标准创建 URI?
【问题讨论】: