Java 将 inputStream 转换为 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19348390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:20:56  来源:igfitidea点击:

Java convert inputStream to URL

javaswingjfxtras

提问by sanchixx

How can I convert an inputStream to a URL? Here is my code:

如何将 inputStream 转换为 URL?这是我的代码:

InputStream song1 = this.getClass().getClassLoader().getResourceAsStream("/songs/BrokenAngel.mp3");
URL song1Url = song1.toUrl(); //<- pseudo code
MediaLocator ml = new MediaLocator(song1Url);
Player p;
try {
    p = Manager.createPlayer(ml);
    p.start();
} catch (NoPlayerException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

采纳答案by Roman Vottner

Note that this approach requires the mp3 to be within your application's sub-directory called songs. You can also use relative pathing for the /songs/BrokenAngel.mp3part (../../ or something like that. But it takes your applications directory as base!

请注意,此方法要求 mp3 位于您的应用程序的名为歌曲的子目录中。您还可以对/songs/BrokenAngel.mp3部分(../../ 或类似的东西)使用相对路径,但它以您的应用程序目录为基础!

    File appDir = new File(System.getProperty("user.dir"));
    URI uri = new URI(appDir.toURI()+"/songs/BrokenAngel.mp3");
    // just to check if the file exists
    File file = new File(uri);
    System.out.println(file.exists())
    URL song1Url = uri.toURL();

回答by Scientist

Try this

尝试这个

InputStream input = new URL("http://www.somewebsite.com/a.txt").openStream();

回答by torquestomp

I think what you want is ClassLoader.findResource(String)

我想你想要的是ClassLoader.findResource(String)

That should return a properly formatted jar:// URL. I haven't tested it myself though, so beware

那应该返回一个格式正确的 jar:// URL。我自己还没有测试过,所以要小心

回答by AlexR

I am not sure you really want to do this. If you need URL for your specific task just do the following:

我不确定你真的想这样做。如果您的特定任务需要 URL,只需执行以下操作:

URL url = this.getClass().getClassLoader().getResource("/songs/BrokenAngel.mp3");

URL url = this.getClass().getClassLoader().getResource("/songs/BrokenAngel.mp3");

If however you retrieve input stream in one part of you code, then pass it to another module and there want to find what was the source URL for this input stream it is "almost" impossible. The problem is that you get BufferedInputStreamthat wraps FileInputStreamthat does not store the information about it source. You cannot retrieve it even using reflection. If you reallywant to do this you can do the following.

但是,如果您在代码的一部分中检索输入流,然后将其传递给另一个模块,并且想要找到此输入流的源 URL 是“几乎”不可能的。问题是你得到的BufferedInputStream包装FileInputStream不存储关于它的信息来源。即使使用反射,您也无法检索它。如果您真的想这样做,您可以执行以下操作。

  1. implement you own UrlInputStream extends InputStreamthe gets into constructor URL, stores it in class varible, creates input stream by invocation of url.openStream()and wraps it.

  2. Now you can use your stream as usual input stream until you have to retrieve the URL. At this point you have to cast it to your UrlInputStreamand call getUrl()method that you will implement.

  1. 实现你拥有UrlInputStream extends InputStream进入构造函数URL,将它存储在类变量中,通过调用创建输入流url.openStream()并包装它。

  2. 现在您可以像往常一样使用您的流,直到您必须检索 URL。此时,您必须将其强制转换为您 将要实现的方法UrlInputStream和调用getUrl()方法。

回答by Chien Cao

Try this

尝试这个

URI uri = new URI("/songs/BrokenAngel.mp3");
URL song1Url = uri.toURL();

MediaLocator ml = new MediaLocator(song1Url);
Player p;
try {
    p = Manager.createPlayer(ml);
    p.start();
} catch (NoPlayerException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}