java 为什么java的URL类不能识别某些协议?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2406518/
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
why does java's URL class not recognize certain protocols?
提问by trinity
URL u=new URL("telnet://route-server.exodus.net");
URL u=new URL("telnet://route-server.exodus.net");
This line is generating :
这条线正在生成:
java.net.MalformedURLException: unknown protocol: telnet
java.net.MalformedURLException: 未知协议: telnet
And i encounter similar problems with other URLs that begin with "news://"
我遇到了其他以“news://”开头的 URL 的类似问题
These are URLs extracted from ODP , so i dont understand why such exceptions arise..
这些是从 ODP 中提取的 URL,所以我不明白为什么会出现这种异常..
回答by Ben S
Issue
问题
Java throws a MalformedURLExceptionbecause it couldn't find a URLStreamHandlerfor that protocol. Check the javadocsof the constructors for the details.
Java 抛出 a 是MalformedURLException因为它找不到URLStreamHandler该协议的 a 。检查构造函数的javadoc以获取详细信息。
Summary:
概括:
Since the URLclass has an openConnectionmethod, the URL class checks to make sure that Java knows how to open a connection of the correct protocol. Without a URLStreamHandlerfor that protocol, Java refuses to create a URLto save you from failure when you try to call openConnection.
由于URL该类有一个openConnection方法,URL 类会检查以确保 Java 知道如何打开正确协议的连接。如果没有URLStreamHandlerfor 该协议,URL当您尝试调用openConnection.
Solution
解决方案
You should probably be using the URIclass if you don't plan on opening a connection of those protocols in Java.
URI如果您不打算在 Java 中打开这些协议的连接,您可能应该使用该类。
回答by Ian C.
Sounds like there's no registered handler for the protocol "telnet" in your application. Since the URL class can be used to open a InputStream to URL it needs to have a registered handler for the protocol to do this work if you're to be allowed to create an object using it.
听起来您的应用程序中没有为协议“telnet”注册的处理程序。由于 URL 类可用于打开 URL 的 InputStream,因此如果允许您使用它创建对象,则需要为协议注册一个处理程序来完成这项工作。
For details on how to add handlers see: http://docs.oracle.com/javase/7/docs/api/java/net/URLStreamHandlerFactory.html
有关如何添加处理程序的详细信息,请参见:http: //docs.oracle.com/javase/7/docs/api/java/net/URLStreamHandlerFactory.html
回答by objects
You're getting that error because java doesn't have a standard protocol handlerfor telnet.
您收到该错误是因为 java 没有用于 telnet的标准协议处理程序。
回答by user207421
The simple answer is that it only doesrecognize certain protocols, and the remainder of the infinity of protocols is not recognized.
简单的答案是,它只是不承认某些协议,而协议的无限的剩余部分不被认可。

