使用 servlet 显示图像
我有一个场景,我需要显示表格(使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题是您将内容类型设置为
"image/png"
但写入 jpegwriteChartAsJPEG
使用
I think problem is you are setting content type as
"image/png"
but writing jpegwriteChartAsJPEG
use