简体   繁体   English

Java将inputStream转换为URL

[英]Java convert inputStream to URL

How can I convert an inputStream to a URL? 如何将inputStream转换为URL? Here is my code: 这是我的代码:

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();
}

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");

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. 但是,如果您在代码的一部分中检索输入流,则将其传递给另一个模块,并且希望找到此输入流的源URL,这几乎是“不可能的”。 The problem is that you get BufferedInputStream that wraps FileInputStream that does not store the information about it source. 问题是你得到BufferedInputStream包装FileInputStream ,它不存储有关它的信息。 You cannot retrieve it even using reflection. 即使使用反射也无法检索它。 If you really want to do this you can do the following. 如果您真的想这样做,您可以执行以下操作。

  1. implement you own UrlInputStream extends InputStream the gets into constructor URL , stores it in class varible, creates input stream by invocation of url.openStream() and wraps it. 实现你自己的UrlInputStream extends InputStream进入构造函数URL ,将它存储在类varible中,通过调用url.openStream()创建输入流并包装它。

  2. Now you can use your stream as usual input stream until you have to retrieve the URL. 现在,您可以将流用作常用输入流,直到您必须检索URL。 At this point you have to cast it to your UrlInputStream and call getUrl() method that you will implement. 此时,您必须将其UrlInputStreamUrlInputStream并调用您将实现的getUrl()方法。

I think what you want is ClassLoader.findResource(String) 我想你想要的是ClassLoader.findResource(String)

That should return a properly formatted jar:// URL. 这应该返回一个格式正确的jar:// URL。 I haven't tested it myself though, so beware 我自己没有测试过,所以要小心

Note that this approach requires the mp3 to be within your application's sub-directory called songs. 请注意,此方法要求mp3位于应用程序的名为songs的子目录中。 You can also use relative pathing for the /songs/BrokenAngel.mp3 part (../../ or something like that. But it takes your applications directory as base! 您还可以使用/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();

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();
}

试试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM