p:graphicImage在谷歌浏览器中消失一段时间后消失

时间:2014-01-23 16:52:51

标签: google-chrome jsf-2 primefaces graphicimage

我使用pf 3.3在jsf 2.0项目中使用主要面的p:graphic image组件。我将其实现为BalusC在诸如this等严峻问题中的解释。它在互联网资源管理器和Firefox上运行良好,但在谷歌浏览器中,图像消失,留下了破碎的图像链接。

在Internet Explorer中;当我保存为图像时,它的名称为untitled.png,没关系。

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

但是在谷歌浏览器中,它的名称为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>

最后是应用程序范围的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的错误吗?

1 个答案:

答案 0 :(得分:1)

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

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

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