display images using servlets
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("i开发者_StackOverflow社区mage/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
I think problem is you are setting content type as "image/png"
but writing jpeg writeChartAsJPEG
use
ChartUtilities.writeChartAsPNG(...)
精彩评论