我正在尝试在 html 页面中显示图像,在<img />
标签中调用 jsp 页面,如下所示。我创建了动态 web 项目,添加index.html
到 webcontent 文件夹timeseries.jsp
下webcontent
→jsp
文件夹下。当我在服务器上运行项目时,添加 apache tomcat 6.0.18,它是同步的,但是当我输入 url 时,localhost:8080/jfree
它在页面上只显示你好医生和图像图标,但那里没有图像。
我的 HTML 是,
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello doctor
<img src="/jfree/jsp/Timeseries.jsp" alt="Progress chart" />
</body>
</html>
我的 JSP 是,
<%@ page import="java.awt.Image" %>
<%@ page import="java.awt.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.awt.BasicStroke"%>
<%@ page import ="org.jfree.ui.ApplicationFrame"%>
<%@ page import="java.io.*" %>
<%@ page import="java.io.File"%>
<%@ page import="org.jfree.chart.*" %>
<%@ page import="org.jfree.chart.axis.*" %>
<%@ page import="org.jfree.chart.entity.*" %>
<%@ page import="org.jfree.chart.labels.*" %>
<%@ page import="org.jfree.chart.plot.*" %>
<%@ page import="org.jfree.chart.renderer.category.*" %>
<%@ page import="org.jfree.chart.urls.*" %>
<%@ page import="org.jfree.data.category.*" %>
<%@ page import="org.jfree.data.general.*" %>
<%@ page import="org.jfree.data.time.Minute"%>
<%@ page import="org.jfree.data.time.Hour"%>
<%@ page import="org.jfree.data.time.TimeSeries"%>
<%@ page import="org.jfree.data.time.TimeSeriesCollection"%>
<%@ page import="org.jfree.data.xy.XYDataset"%>
<%@ page import="org.jfree.chart.plot.XYPlot"%>
<%@ page import="org.jfree.chart.renderer.xy.StandardXYItemRenderer"%>
<%@ page import="org.jfree.chart.renderer.xy.XYItemRenderer"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import=java.sql.Statement" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="java.awt.image.BufferedImage"%>
<%
try
{
File image = File.createTempFile("image", "tmp");
//chart class instance
Fms fm = new Fms("Graph");
JFreeChart chart = fm.createChart(fm.dataset);
ChartUtilities.saveChartAsPNG(image, chart, 500, 400);
//get input stream
FileInputStream fileInStream = new FileInputStream(image);
//output stream foe returning chart as image
OutputStream outStream = response.getOutputStream();
long fileLength;
byte[] byteStream;
fileLength = image.length();
byteStream = new byte[(int)fileLength];
//read chart image
fileInStream.read(byteStream, 0, (int)fileLength);
//returns chart image whenever called
response.setContentType("image/png");
response.setContentLength((int)fileLength);
response.setHeader("Cache-Control","no-store,no-cache, must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
fileInStream.close();
outStream.write(byteStream);
outStream.flush();
outStream.close();
}
catch (IOException e)
{
System.err.println("Problem occurred creating chart.");
}
%>
<%!
public class Fms extends ApplicationFrame {
//Main class
XYDataset dataset= null;
public Fms(final String title) {
super(title);
dataset= createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(700, 570));
chartPanel.setMouseZoomable(true, false);
setContentPane(chartPanel);
}
//chart creation method
JFreeChart createChart(final XYDataset dataset) {
final JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Speed Chart",
"Time",
"Speed",
dataset,
true,
true,
false
);
chart.setBackgroundPaint(Color.white);
final XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(false);
final XYItemRenderer renderer = plot.getRenderer();
if (renderer instanceof StandardXYItemRenderer) {
final StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
rr.setShapesFilled(true);
renderer.setSeriesStroke(0, new BasicStroke(1.0f));
renderer.setSeriesStroke(1, new BasicStroke(1.0f));
}
final DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd:MM"));
try{
final ChartRenderingInfo info = new ChartRenderingInfo
(new StandardEntityCollection());
final File file1 = new File("c:/Documents and Settings/accounts/WebApplication2/web/barchart.png");
ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
}catch(Exception e){ }
return chart;
}
//data set generation method
private XYDataset createDataset() {
final TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.setDomainIsPointsInTime(true);
final TimeSeries s1 = new TimeSeries("Series 1", Minute.class);
s1.add(new Minute(0, 0, 7, 7, 2003), 10.2);
s1.add(new Minute(30, 12, 7, 8, 2003), 23.0);
s1.add(new Minute(15, 14, 7, 9, 2003), 48.0);
final TimeSeries s2 = new TimeSeries("Series 2", Minute.class);
s2.add(new Minute(0, 0, 7, 7, 2003), 23.0);
s2.add(new Minute(30, 12, 7, 8, 2003), 9.0);
s2.add(new Minute(15, 14, 7, 9, 2003), 36.0);
dataset.addSeries(s1);
dataset.addSeries(s2);
return dataset;
}
}
%>
但它没有在jsp中显示图像?有什么帮助吗?