0

p:graphic image在 pf 3.3 的 jsf 2.0 项目中使用素面组件。我按照 BalusC 在这样的严重问题中解释的方式实现了。它在 Internet Explorer 和 Firefox 上运行良好,但在 google chrome 中,图像消失,留下一个损坏的图像链接。

在 Internet Explorer 中;当我保存为图像时,它被命名untitled.png并且没关系。

在 Internet Explorer 中;当我保存为图像时,它被命名dynamiccontent.xhtml.png并且没关系。

但在 google chrome 中,它的名称dynamiccontent.xhtml是,即使我更改了扩展名,图像也不可见。

我用java图形库创建图像

BufferedImage bufferedimage = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedimage.createGraphics();
....
g2d.dispose();

ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedimage, "png", os);

return os;

我的xhtml就像:

<p:graphicImage value="#{imagesBean.image}">
    <f:param name="lilw" value="#{reportsLoadSheetBean.loadedIndexLandingWeight}" />
    <f:param name="litow" value="#{reportsLoadSheetBean.loadedIndexTakeOffWeight}" />
    <f:param name="lizfw" value="#{reportsLoadSheetBean.loadedIndexZeroFuelWeight}" />
    <f:param name="tl" value="#{reportsLoadSheetBean.topLimit}" />
    <f:param name="zfwf" value="#{reportsLoadSheetBean.zeroFuelWeightForward}" />
    <f:param name="zfwa" value="#{reportsLoadSheetBean.zeroFuelWeightAfter}" />
    <f:param name="towf" value="#{reportsLoadSheetBean.takeOffWeightForward}" />
    <f:param name="towa" value="#{reportsLoadSheetBean.takeOffWeightAfter}" />
    <f:param name="lwf" value="#{reportsLoadSheetBean.landingWeightForward}" />
    <f:param name="lwa" value="#{reportsLoadSheetBean.landingWeightAfter}" />
    <f:param name="width" value="600" />
    <f:param name="height" value="200" />
</p:graphicImage>

最后是 Application 范围的 bean,它获取图像并将其发送到接口:

public StreamedContent getImage() throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();

    if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
        // So, we're rendering the view. Return a stub StreamedContent so
        // that it will generate right URL.
        return new DefaultStreamedContent();
    } else {
        String lilw = context.getExternalContext().getRequestParameterMap().get("lilw");
        String litow = context.getExternalContext().getRequestParameterMap().get("litow");
        String lizfw = context.getExternalContext().getRequestParameterMap().get("lizfw");
        String tl = context.getExternalContext().getRequestParameterMap().get("tl");
        String zfwf = context.getExternalContext().getRequestParameterMap().get("zfwf");
        String zfwa = context.getExternalContext().getRequestParameterMap().get("zfwa");
        String towf = context.getExternalContext().getRequestParameterMap().get("towf");
        String towa = context.getExternalContext().getRequestParameterMap().get("towa");
        String lwf = context.getExternalContext().getRequestParameterMap().get("lwf");
        String lwa = context.getExternalContext().getRequestParameterMap().get("lwa");

        List<Number> zeroFuelWeightIntervals = new ArrayList<Number>();
        List<Number> takeOffWeightIntervals = new ArrayList<Number>();
        List<Number> landingWeightIntervals = new ArrayList<Number>();

        zeroFuelWeightIntervals.add(Double.valueOf(zfwf));
        zeroFuelWeightIntervals.add(Double.valueOf(zfwa));
        zeroFuelWeightIntervals.add(Double.valueOf(tl));

        takeOffWeightIntervals.add(Double.valueOf(towf));
        takeOffWeightIntervals.add(Double.valueOf(towa));
        takeOffWeightIntervals.add(Double.valueOf(tl));

        landingWeightIntervals.add(Double.valueOf(lwf));
        landingWeightIntervals.add(Double.valueOf(lwa));
        landingWeightIntervals.add(Double.valueOf(tl));

        int width = Integer.parseInt(context.getExternalContext().getRequestParameterMap().get("width"));
        int height = Integer.parseInt(context.getExternalContext().getRequestParameterMap().get("height"));

        IdealIntervalPointDiagram diagram = new IdealIntervalPointDiagram(
                "ZFL", Double.valueOf(lizfw), zeroFuelWeightIntervals, 
                "TOW", Double.valueOf(litow), takeOffWeightIntervals, 
                "LAW", Double.valueOf(lilw), landingWeightIntervals,
                width, height);

        ByteArrayOutputStream os = new ByteArrayOutputStream();

        try {
            os = diagram.paint();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");
    }

可能是关于primefaces的错误吗?

4

1 回答 1

1

我发现了问题。这是关于谷歌浏览器在包括图像在内的响应中严格检查content-length标题,并且因为服务器压缩了图像;实际内容大小和标题上写的大小不匹配。其他主流浏览器会忽略这种不匹配,但 google chrome 不会。所以我打电话

FacesContext.getCurrentInstance().getExternalContext().setResponseContentLength(os.size());

在返回图像之前,现在它在谷歌浏览器中也可以正常工作。

于 2014-01-24T14:19:15.760 回答