【问题标题】:How to set Content-Length in response header for Jfree chart?如何在 Jfree 图表的响应标头中设置 Content-Length?
【发布时间】:2019-05-15 13:25:00
【问题描述】:

我有一个 servlet 正在创建 jfree 图表。默认情况下,它以分块形式返回传输编码。但我必须在响应头中设置 Content-Length。

final JFreeChart chart = ChartController.createChart();

          final int chartWidth = ChartUtils.calculateWidth(request);
          final int chartHeight = ChartHelper.getQuickViewChartHeight();

          final ServletOutputStream out = response.getOutputStream();
          response.setContentType("image/png");       
          ChartUtilities.writeChartAsPNG(out, chart, chartWidth, chartHeight);

【问题讨论】:

    标签: httpresponse jfreechart


    【解决方案1】:

    正如here 建议的那样,设置内容长度标头没有意义并且也使用分块传输编码。无论哪种情况,如here 所述,您都可以使用ChartUtilities.encode() 来确定编码图像数组的字节长度:

    byte[] b = ChartUtilities.encode(chart.createBufferedImage(chartWidth, chartHeight));
    int imageLength = b.length;
    

    以后可以write()将编码后的图片输出到流中:

    out.write(b);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      相关资源
      最近更新 更多