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();
}

我们可以将 bImageFromConvert 值输入<h:graphicImage />还是在显示之前获取 BufferedImage 之前还有其他步骤<h:graphicImage value="?"/>

4

2 回答 2

0

不幸的是,这里还没有通用的解决方案。但是检测 ResourceHandler 您可以以官方方式实现这一点(自 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);
        }
    }
}

并在 faces-config.xml 中注册:

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

通过 /faces/javax.faces.resource/test.gif?ln=test 或访问资源

此解决方案既适用于应用程序本身,也适用于基础应用程序 WAR 中包含的任何 JSF 库。

于 2013-05-17T16:25:19.660 回答
0

另一种解决方法是使用 Base64 图像编码,例如以这种方式:

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

(此代码需要 Java 7)

然后在经典的 HTML<img>标签上使用它:

<img src="${myBean.myImageBase64}" alt="" />
于 2016-06-21T08:27:36.073 回答