trinity
trinity

Reputation: 10484

Why does Java's URL class not recognize certain protocols?

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..

Upvotes: 36

Views: 26198

Answers (5)

Bala kumar
Bala kumar

Reputation: 15

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!

Upvotes: 1

Ben S
Ben S

Reputation: 69342

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.

Upvotes: 59

Ian C.
Ian C.

Reputation: 3963

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

Upvotes: 3

user207421
user207421

Reputation: 310909

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

Upvotes: 2

objects
objects

Reputation: 8677

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

Upvotes: 2

Related Questions