在Java中查找URI对应的协议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19430737/
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 17:05:08  来源:igfitidea点击:

find protocol corresponding to URI in Java

javaurluriinputstream

提问by M314

I have URI object in Java. I want to convert it to InputStream, but the conversion should depend on the protocol. I can make it this way if my URI is http://somepath.com/mysuperfile.xsl:

我在 Java 中有 URI 对象。我想将其转换为 InputStream,但转换应取决于协议。如果我的 URI 是http://somepath.com/mysuperfile.xsl

return myURI.toURL().openConnection().getInputStream();

or this way if my uri is file:///somepath/mysuperfile.xsl:

或者这样,如果我的 uri 是file:///somepath/mysuperfile.xsl

return new FileInputStream(Paths.get(myURI).toFile());

or maybe even some another way. I can try to check it manually, but do Java have some nice/proper way to check it, maybe using that new java.nio.*package?

或者甚至是另一种方式。我可以尝试手动检查它,但是 Java 是否有一些不错/正确的方法来检查它,也许使用那个新java.nio.*包?

采纳答案by Asaph

Every URI is defined as consisting of four parts, as follows:

[scheme name] : [hierarchical part] [[ ? query ]] [[ # fragment ]]

每个 URI 被定义为由四部分组成,如下所示:

[scheme name] : [hierarchical part] [[ ? query ]] [[ # fragment ]]

If the thing you want is the scheme name (which roughly translates to protocol), just use

如果您想要的是方案名称(大致翻译为协议),只需使用

switch ( myURI.getScheme() ) {
  case "http":
    return myURI.toURL().openConnection().getInputStream();
  case "ftp":
    // do something   
  case "file":
    return new FileInputStream( Paths.get(myURI).toFile() );
}

http://docs.oracle.com/javase/6/docs/api/java/net/URI.html#getScheme%28%29

http://docs.oracle.com/javase/6/docs/api/java/net/URI.html#getScheme%28%29

or, if you just want to generate an InputStreamwithoutdifferentiating the scheme, simply use

或者,如果您只想生成一个InputStream而不区分方案,只需使用

return myURI.toURL().openStream();

or

或者

return myURI.toURL().openConnection().getInputStream();

(as you already did for HTTP protocol/scheme)

(正如您已经为 HTTP 协议/方案所做的那样)

回答by Daan Luttik

you can check the characters at the start of the string with the startsWith(String prefix) function, it is www. or http:// use the first method otherwise use the second method.

您可以使用startsWith(String prefix) 函数检查字符串开头的字符,它是www。或 http:// 使用第一种方法,否则使用第二种方法。

回答by Asaph

No need to special case file URIs. The same code works for either case. I just tested it with the following little scratch program:

无需特殊情况文件 URI。相同的代码适用于任何一种情况。我刚刚使用以下小程序对其进行了测试:

URIReadTest.java

URIReadTest.java

import java.io.*;
import java.net.*;

public class URIReadTest {
    public static void main(String[] args) throws Exception {
        URI uri = new URI("file:///tmp/test.txt");
        InputStream in = uri.toURL().openConnection().getInputStream();
        // or, more concisely:
        // InputStream in = uri.toURL().openStream();
        int b;
        while((b = in.read()) != -1) {
            System.out.print((char) b);
        }
        in.close();
    }
}

Make a /tmp/test.txton your system and you'll see the contents of it printed out when you compile and run the above code.

/tmp/test.txt在你的系统上创建一个,当你编译和运行上面的代码时,你会看到它的内容被打印出来。