2
byte [] imageInByte = event.getFile().getContents();
InputStream in = new ByteArrayInputStream(imageInByte);
try {
    BufferedImage bImageFromConvert = ImageIO.read(in);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Can we take the bImageFromConvert value into <h:graphicImage /> or there is other step before getting a BufferedImage before displaying inside <h:graphicImage value="?"/>

DarkVision
  • 1,373
  • 3
  • 20
  • 33

2 Answers2

0

Unfortunately there is no generic solution here, yet. But instrumenting ResourceHandler you can implement this an official way (since JSF 2):

public class TestResourceHandler extends ResourceHandlerWrapper {
    public static final class TestResource extends Resource {
        @Override
        public InputStream getInputStream() throws IOException {
            return null;
        }

        @Override
        public Map<String, String> getResponseHeaders() {
            return new HashMap<>();
        }

        @Override
        public String getRequestPath() {
            FacesContext context = FacesContext.getCurrentInstance();
            return context.getApplication().getViewHandler().getResourceURL(
                context, "/faces" + RESOURCE_IDENTIFIER + "/test.gif?ln=test");
        }

        @Override
        public URL getURL() {
            return null;
        }

        @Override
        public boolean userAgentNeedsUpdate(FacesContext context) {
            return true;
        }
    }

    private ResourceHandler wrapped;

    public TestResourceHandler(ResourceHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public ResourceHandler getWrapped() {
        return wrapped;
    }

    @Override
    public Resource createResource(String resourceName, String libraryName) {
        if ("test".equals(libraryName))
            return new TestResource();
        return super.createResource(resourceName, libraryName);
    }

    @Override
    public boolean libraryExists(String libraryName) {
        if ("test".equals(libraryName))
            return true;
        return super.libraryExists(libraryName);
    }

    @Override
    public void handleResourceRequest(FacesContext context) throws IOException {
        String library = context.getExternalContext().getRequestParameterMap().get("ln");
        if ("test".equals(library)) {
            BufferedImage image = ImageIO.read(getClass().getResource("/info.png"));
            context.getExternalContext().setResponseContentType("image/gif");
            ImageIO.write(image, "gif", context.getExternalContext().getResponseOutputStream());
            context.responseComplete();
        } else {
            wrapped.handleResourceRequest(context);
        }
    }
}

And registered in the faces-config.xml:

<faces-config>
    <application>
        ...
        <resource-handler>com.intersult.test.util.TestResourceHandler</resource-handler>
    </application>
    ...
</faces-config>

Access the resource via /faces/javax.faces.resource/test.gif?ln=test or

This solution works both in the application itself and in any JSF-library included in the base application WAR.

Tires
  • 1,529
  • 16
  • 27
0

An alternative workaround is to use Base64 image encoding, for example in this way:

byte[] imageBytes = Files.readAllBytes(myFile.toPath());
this.myImageBase64 = "data:" + contentType + ";base64," + DatatypeConverter.printBase64Binary(imageBytes);

(this code requires Java 7)

and then use it on a classic HTML <img> tag:

<img src="${myBean.myImageBase64}" alt="" />
xonya
  • 2,146
  • 29
  • 37