Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法将数据方案支持(RFC 2397)添加到java.net.url?_Java - Fatal编程技术网

有没有办法将数据方案支持(RFC 2397)添加到java.net.url?

有没有办法将数据方案支持(RFC 2397)添加到java.net.url?,java,Java,看起来java.net.URL原则上可以使用自定义URLHandlers进行扩展,而且它目前不支持数据:URL 我正在使用一个第三方库,它使用由字符串构造的URLs来检索图像,并希望直接传递图像数据。是否有任何人可以推荐的合适处理程序的现有实现?如果仔细阅读RFC 2397,您将看到“数据”URL方案的定义如下: data:[<mediatype>][;base64],<data> byte[] fakeImage = new byte[1]; StringBuilde

看起来
java.net.URL
原则上可以使用自定义
URLHandler
s进行扩展,而且它目前不支持
数据:
URL


我正在使用一个第三方库,它使用由字符串构造的
URL
s来检索图像,并希望直接传递图像数据。是否有任何人可以推荐的合适处理程序的现有实现?

如果仔细阅读RFC 2397,您将看到“数据”URL方案的定义如下:

data:[<mediatype>][;base64],<data>
byte[] fakeImage = new byte[1];
StringBuilder sb = new StringBuilder();

// acquired from file extension
String mimeType = "image/jpg";
sb.append("data:");
sb.append(mimeType);
sb.append(";base64,");
sb.append(Base64.getEncoder().encodeToString(fakeImage));
现在有一个有趣的部分:您必须注册自己的协议处理程序,但是它定义得很好:

If this is the first URL object being created with the specifiedprotocol, a stream protocol handler object, an instance ofclass URLStreamHandler, is created for that protocol: 
1.If the application has previously set up an instance of URLStreamHandlerFactory as the stream handler factory,then the createURLStreamHandler method of that instanceis called with the protocol string as an argument to create thestream protocol handler. 
2.If no URLStreamHandlerFactory has yet been set up,or if the factory's createURLStreamHandler methodreturns null, then the constructor finds thevalue of the system property: 
         java.protocol.handler.pkgs

If the value of that system property is not null,it is interpreted as a list of packages separated by a verticalslash character '|'. The constructor tries to loadthe class named: 
         <package>.<protocol>.Handler

where <package> is replaced by the name of the packageand <protocol> is replaced by the name of the protocol.If this class does not exist, or if the class exists but it is nota subclass of URLStreamHandler, then the next packagein the list is tried. 
3.If the previous step fails to find a protocol handler, then theconstructor tries to load from a system default package. 
         <system default package>.<protocol>.Handler

If this class does not exist, or if the class exists but it is not asubclass of URLStreamHandler, then a MalformedURLException is thrown.
为了实现此目的,您必须在名为
Handler
的类中创建名为
stackoverflow.data
的包,并包含以下内容:

package stackoverflow.data;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

public class Handler extends URLStreamHandler {

    @Override
    protected URLConnection openConnection(URL u) throws IOException {
        return null;
    }

}
然后,您可以创建一个新的URL,而不会出现任何异常:

URL url = new URL(sb.toString());

我不知道你为什么需要这样做,但你在这里。

谢谢,我明白。开放连接的实际实现正是我所希望的,我认为一定有现有的库。我使用的第三方库获取字符串,将其解释为URL并从中读取图像。因此,我不只是需要构造URL对象;我需要它们来实际交付图像数据。因为我不知道您的第三方库如何使用这些URL(它们甚至调用openConnection吗?),所以您可能需要实现您需要的所有功能。事实上,我觉得使用java.net.URL类来实现这一点很奇怪,你确定他们没有合适的URL类吗?因为为数据字符串创建URL在这里看起来不是很直观,因为URL类实际上是为了打开某些东西而设计的,但是静态字符串实际上不是您可以打开的东西。它们基本上会将它们传递给ImageIO,ImageIO会调用openConnection。事实上,他们使用URL是相当愚蠢的,我基本上是在尝试解决这个愚蠢的API,这样我就可以使用我的渲染图像。至于不能打开的静态字符串,这就是RFC2397背后的全部思想,不是吗?我很惊讶现在还没有人支持它。啊,现在它有点道理了。您不必向它们传递数据方案URL,只需将字节[]从解码的base64字符串存储到文件系统并交出文件URL。我想避免使用大量的小型图像文件。无论如何谢谢你!
URL url = new URL(sb.toString());