使用 servlet 显示图像

发布于 2024-11-18 11:46:12 字数 1441 浏览 5 评论 0原文

我有一个场景,我需要显示表格(使用 JTable 生成),将其转换为 png 图像,然后使用 servlet 显示它们。

以下是 servlet 中用于显示表格的代码:

{
        table = u.generateTableChart("datamonth");    
        saveToServlet(table, table.getTableHeader(), p_resp);
}

void saveToServlet(JTable table, JTableHeader header,
            HttpServletResponse p_resp)
    {

        int w = Math.max(table.getWidth(), header.getWidth());
        int h = table.getHeight() + header.getHeight();
        OutputStream out = null;
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bi.createGraphics();
        header.paint(g2);
        g2.translate(0, header.getHeight());
        table.paint(g2);
        g2.dispose();
        try {
            p_resp.setContentType("image/png");
            out = p_resp.getOutputStream();
            ImageIO.write(bi, "png", out);

        } catch (IOException ioe) {
            System.out.println("write: " + ioe.getMessage());
        }
    }

当我尝试在网页上查看它时,这似乎会引发此错误 该图像无法显示,因为它有太多错误。

我能够将文件存储在临时位置,并且能够看到图表。

对于使用 JFreeCharts 生成的图表,我正在使用这个:

 ServletOutputStream out = null;

    try {
        out = p_resp.getOutputStream();
        p_resp.setContentType("image/png");
        chart=u.genarateLineChart(m_martiniInstance);
        ChartUtilities.writeChartAsJPEG(out, chart, 625, 500); 
    }

但仍然不走运,

提前致谢, 巴维亚

I have a scenario where I need to display tables(generated using JTable) converted it into a png image and then display them using servlets.

The following is the code in the servlet to display the table :

{
        table = u.generateTableChart("datamonth");    
        saveToServlet(table, table.getTableHeader(), p_resp);
}

void saveToServlet(JTable table, JTableHeader header,
            HttpServletResponse p_resp)
    {

        int w = Math.max(table.getWidth(), header.getWidth());
        int h = table.getHeight() + header.getHeight();
        OutputStream out = null;
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bi.createGraphics();
        header.paint(g2);
        g2.translate(0, header.getHeight());
        table.paint(g2);
        g2.dispose();
        try {
            p_resp.setContentType("image/png");
            out = p_resp.getOutputStream();
            ImageIO.write(bi, "png", out);

        } catch (IOException ioe) {
            System.out.println("write: " + ioe.getMessage());
        }
    }

This seems to be throwing this error when I try to view it on the web page
The image cannot be displayed as it has too many errors.

I am able to store the file in a temporary location and I am able to see the graph.

For Charts generated using JFreeCharts I am using this :

 ServletOutputStream out = null;

    try {
        out = p_resp.getOutputStream();
        p_resp.setContentType("image/png");
        chart=u.genarateLineChart(m_martiniInstance);
        ChartUtilities.writeChartAsJPEG(out, chart, 625, 500); 
    }

and still not luck

Thanks in advance,
Bhavya

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2024-11-25 11:46:12

我认为问题是您将内容类型设置为 "image/png" 但写入 jpeg writeChartAsJPEG
使用

ChartUtilities.writeChartAsPNG(...)

I think problem is you are setting content type as "image/png" but writing jpeg writeChartAsJPEG
use

ChartUtilities.writeChartAsPNG(...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文