为什么 Java 的 URL 类无法识别某些协议?

发布于 2024-08-24 04:49:57 字数 253 浏览 9 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟若柳尘 2024-08-31 04:49:58

问题

Java 抛出 MalformedURLException,因为它找不到该协议的 URLStreamHandler。有关详细信息,请检查构造函数的 javadocs

总结

由于 URL 类具有 openConnection 方法,因此 URL 类会进行检查以确保 Java 知道如何打开正确协议的连接。如果没有该协议的 URLStreamHandler,Java 会拒绝创建 URL,以避免在您尝试调用 openConnection 时失败。

解决方案

您可能应该使用 URI类(如果您不打算在 Java 中打开这些协议的连接)。

Issue

Java throws a MalformedURLException because it couldn't find a URLStreamHandler for that protocol. Check the javadocs of the constructors for the details.

Summary

Since the URL class has an openConnection method, the URL class checks to make sure that Java knows how to open a connection of the correct protocol. Without a URLStreamHandler for that protocol, Java refuses to create a URL to save you from failure when you try to call openConnection.

Solution

You should probably be using the URI class if you don't plan on opening a connection of those protocols in Java.

躲猫猫 2024-08-31 04:49:58

听起来您的应用程序中没有为协议“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

美人迟暮 2024-08-31 04:49:58

您收到该错误是因为 java 没有标准的 协议处理程序远程登录。

You're getting that error because java doesn't have a standard protocol handler for telnet.

怂人 2024-08-31 04:49:58

简单的答案是,它仅识别某些协议,而无法识别其余的无限协议。

The simple answer is that it only does recognize certain protocols, and the remainder of the infinity of protocols is not recognized.

穿透光 2024-08-31 04:49:58

如果图像 URL 以“data”开头,则意味着图像数据嵌入在 HTML 页面本身中,而不是存储在可通过 URL 访问的远程服务器上。因此,您无法使用标准 HTTP 连接下载图像。所以,base64机制对我们有帮助。

图像源 URL : data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAWAUNoinFRBWASIUA........AAAAElFTkSuQmCC

要下载图像,可以使用以下代码:

// Get the image source data
String imageData = webElement.getAttribute("src");

// Extract the image data and file extension from the data URL
String[] parts = imageData.split(",");
String mimeType = parts[0].split(":")[1];
String base64Data = parts[1];
String fileExtension = "";

if (mimeType.equals("image/jpeg")) {
    fileExtension = ".jpg";
} else if (mimeType.equals("image/png")) {
    fileExtension = ".png";
} else if (mimeType.equals("image/gif")) {
    fileExtension = ".gif";
} else {
    // Unsupported image format
    throw new IOException("Unsupported image format");
}

// Set the output file path and stream. Here, we save the image file.
String outputPath = "C:/images/image" + fileExtension;
FileOutputStream outputStream = new FileOutputStream(outputPath);

// Close the output stream
outputStream.close();

该代码首先从“data”URL 中提取图像数据并将其拆分成其 MIME 类型和 base64 编码的数据组件。然后,在解码 Base64 编码的图像数据后,它会根据 MIME 类型确定文件扩展名,并将图像保存到磁盘上的文件中。请注意,您需要处理解码和文件 I/O 过程中可能发生的任何异常。

要使用此代码,除了我在之前的答案中提到的类之外,您还需要导入以下类:

import java.io.File;
import java.util.Base64;

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:

// Get the image source data
String imageData = webElement.getAttribute("src");

// Extract the image data and file extension from the data URL
String[] parts = imageData.split(",");
String mimeType = parts[0].split(":")[1];
String base64Data = parts[1];
String fileExtension = "";

if (mimeType.equals("image/jpeg")) {
    fileExtension = ".jpg";
} else if (mimeType.equals("image/png")) {
    fileExtension = ".png";
} else if (mimeType.equals("image/gif")) {
    fileExtension = ".gif";
} else {
    // Unsupported image format
    throw new IOException("Unsupported image format");
}

// Set the output file path and stream. Here, we save the image file.
String outputPath = "C:/images/image" + fileExtension;
FileOutputStream outputStream = new FileOutputStream(outputPath);

// Close the output stream
outputStream.close();

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:

import java.io.File;
import java.util.Base64;

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文