【问题标题】:creating URI for media为媒体创建 URI
【发布时间】: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?

【问题讨论】:

    标签: java javafx uri media


    【解决方案1】:

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

    做你需要的?

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      相关资源
      最近更新 更多