How can I return a URLConnection given a byte array

548 views Asked by At

I need to render an html document that can have images stored both internally and externally. To render I use a library that will load these images by their urls (as referenced in the html).

What I'm doing is:

  1. for external images I use http:// urls normally.

  2. for images in the classpath I use a "classpath://" protocol in the url so I can redirect to it when the library is trying to load.

To do as in 2. I extend java.net.URLStreamHandler like follows:

public class ClasspathUrlHandler extends URLStreamHandler
{
    @Override
    protected URLConnection openConnection(URL relativeUrl) throws IOException
    {
        ClassLoader classLoader = getClass().getClassLoader();
        URL absoluteUrl = classLoader.getResource(relativeUrl.getPath());
        return absoluteUrl.openConnection();
    }
}

My problem now is that some images are stored as blob in the database and I can only access them as byte arrays. I can't get an absolute url to them like in the classpath case.

Is there a way I can create a URLConnection object based on a byte[]?

Note: I want URLConnection because that's what openConnection() in URLStreamHandler returns, like in the example.

0

There are 0 answers