为什么 Java 的 URL 类无法识别某些协议?
URL u=new URL("telnet://route-server.exodus.net");
此行正在生成:
java.net.MalformedURLException: unknown protocol: telnet
并且我遇到以“news://”开头的其他 URL 的类似问题
这些是从 ODP 中提取的 URL,所以我不明白为什么会出现此类异常。
URL u=new URL("telnet://route-server.exodus.net");
This line is generating :
java.net.MalformedURLException: unknown protocol: telnet
And I encounter similar problems with other URLs that begin with "news://"
These are URLs extracted from ODP, so I don't understand why such exceptions arise..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题
Java 抛出
MalformedURLException
,因为它找不到该协议的URLStreamHandler
。有关详细信息,请检查构造函数的 javadocs 。总结
由于
URL
类具有openConnection
方法,因此 URL 类会进行检查以确保 Java 知道如何打开正确协议的连接。如果没有该协议的URLStreamHandler
,Java 会拒绝创建URL
,以避免在您尝试调用openConnection
时失败。解决方案
您可能应该使用
URI
类(如果您不打算在 Java 中打开这些协议的连接)。
Issue
Java throws a
MalformedURLException
because it couldn't find aURLStreamHandler
for that protocol. Check the javadocs of the constructors for the details.Summary
Since the
URL
class has anopenConnection
method, the URL class checks to make sure that Java knows how to open a connection of the correct protocol. Without aURLStreamHandler
for that protocol, Java refuses to create aURL
to save you from failure when you try to callopenConnection
.Solution
You should probably be using the
URI
class if you don't plan on opening a connection of those protocols in Java.听起来您的应用程序中没有为协议“telnet”注册的处理程序。由于 URL 类可用于打开 URL 的 InputStream,因此如果允许您使用它创建对象,则需要为协议注册一个处理程序来完成此工作。
有关如何添加处理程序的详细信息,请参阅: http:// /docs.oracle.com/javase/7/docs/api/java/net/URLStreamHandlerFactory.html
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.
For details on how to add handlers see: http://docs.oracle.com/javase/7/docs/api/java/net/URLStreamHandlerFactory.html
您收到该错误是因为 java 没有标准的 协议处理程序远程登录。
You're getting that error because java doesn't have a standard protocol handler for telnet.
简单的答案是,它仅识别某些协议,而无法识别其余的无限协议。
The simple answer is that it only does recognize certain protocols, and the remainder of the infinity of protocols is not recognized.
如果图像 URL 以“data”开头,则意味着图像数据嵌入在 HTML 页面本身中,而不是存储在可通过 URL 访问的远程服务器上。因此,您无法使用标准 HTTP 连接下载图像。所以,base64机制对我们有帮助。
图像源 URL : data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAWAUNoinFRBWASIUA........AAAAElFTkSuQmCC
要下载图像,可以使用以下代码:
该代码首先从“data”URL 中提取图像数据并将其拆分成其 MIME 类型和 base64 编码的数据组件。然后,在解码 Base64 编码的图像数据后,它会根据 MIME 类型确定文件扩展名,并将图像保存到磁盘上的文件中。请注意,您需要处理解码和文件 I/O 过程中可能发生的任何异常。
要使用此代码,除了我在之前的答案中提到的类之外,您还需要导入以下类:
java.util.Base64 类用于解码 Base64 编码的图像数据。 java.io.File 类用于表示磁盘上的输出文件。
我希望这可以帮助别人!
If the image URL begins with "data", it means that the image data is embedded in the HTML page itself, rather than being stored on a remote server that can be accessed via a URL. Therefore, you cannot download the image using a standard HTTP connection. So, the base64 mechanism helps us.
Image Source URL : data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAWAUNoinFRBWASIUA........AAAAElFTkSuQmCC
To download the image, the below code can be used:
This code first extracts the image data from the "data" URL and splits it into its MIME type and base64-encoded data components. It then determines the file extension based on the MIME type and saves the image to a file on disk, after decoding the base64-encoded image data. Note that you will need to handle any exceptions that may occur during the decoding and file I/O processes.
To use this code, you will need to import the following classes in addition to the ones I mentioned in my previous answer:
The java.util.Base64 class is used to decode the base64-encoded image data. The java.io.File class is used to represent the output file on disk.
I hope this might help someone!