jfreeChart路径问题

Ginie 广州一康医疗设备实业有限公司 项目经理  2010-09-27 02:27:50
按照网上的说法改了saveChartAsPNG()方法,写了个BarChart类,内容如下

public class BarChart extends ServletUtilities {



protected static void createTempDir()
{
String tempDirName = "E:/myworkspace/st2/WebRoot/upload1";
if (tempDirName == null)
{
throw new RuntimeException("Temporary directory system property " + "(java.io.tmpdir) is null.");
}

// create the temporary directory if it doesn't exist
File tempDir = new File(tempDirName);
if (!tempDir.exists())
{
tempDir.mkdirs();
}
}

public static String saveChartAsPNG(JFreeChart chart, int width, int height,
ChartRenderingInfo info, HttpSession session) throws IOException
{
if (chart == null)
{
throw new IllegalArgumentException("Null 'chart' argument.");
}
createTempDir();
String prefix = ServletUtilities.getTempFilePrefix();
if (session == null)
{
prefix = ServletUtilities.getTempOneTimeFilePrefix();
}
File tempFile = File.createTempFile(prefix, ".png", new File("E:/myworkspace/st2/WebRoot/upload1"));
ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
if (session != null)
{
ServletUtilities.registerChartForDeletion(tempFile, session);
}

System.out.println("tempFile.getName()=" + tempFile.getName());

return tempFile.getName();


}

}


随后在jsp页面中这样写代码的

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset"%>
<%@ page import="org.jfree.chart.ChartFactory"%>
<%@ page
import="org.jfree.chart.JFreeChart,org.jfree.chart.servlet.ServletUtilities,com.test.jfreechart.BarChart"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>
<s:form namespace="/">

<%
DefaultPieDataset dpd = new DefaultPieDataset();

dpd.setValue("管理人员", 25);
dpd.setValue("市场人员", 25);
dpd.setValue("开发人员", 45);
dpd.setValue("其他人员", 10);

JFreeChart chart = ChartFactory.createPieChart("某公司组织结构图", dpd,
true, false, false);

String fileName = "";


fileName = BarChart
.saveChartAsPNG(chart, 800, 600, session);
System.out.println("fileName=" + fileName);

String url = request.getContextPath() + "/DisplayChart?filename="
+ fileName;

System.out.println("url =" + url);
%>

<img src="<%=url%>" width="500" height="300">
</s:form>
</body>
</html>




运行结果是
它还是在Tomcat的temp目录下生成图片,感觉我写的类都没用一样。而且还报了警告,内容是
警告: No configuration found for the specified action: '/jfreeChart2.jsp' in namespace: '/'. Form action defaulting to 'action' attribute's literal value.
fileName=jfreechart-58472.png
url =/st2/DisplayChart?filename=jfreechart-58472.png
2010-9-27 14:24:17 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: No configuration found for the specified action: '/jfreeChart2.jsp' in namespace: '/'. Form action defaulting to 'action' attribute's literal value.
2010-9-27 14:24:17 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find action or result
There is no Action mapped for namespace / and action name DisplayChart. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:178)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)
我有点看不懂它的警告了,它说namespace的问题,可是页面中没有用到action啊,我的web.xml里是这样配置的

<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>
org.jfree.chart.servlet.DisplayChart
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>


我的Tomcat的路径是D:\tomcat\Tomcat 6.0\temp
项目的路径是E:\myworkspace\st2\WebRoot
结果运行结果在网页上查看原代码的src是<img src="/st2/DisplayChart?filename=jfreechart-58472.png" width="500" height="300">

这是怎么回事啊,麻烦各位了
...全文
434 点赞 收藏 18
写回复
18 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Codefans_Fan 2011-09-20
学习了 我用框架 显示不出来了
回复
Ginie 2010-09-28
呵呵。pengzhistar 谢谢你来,确实是我粗心了,少了个参数,现在可以了,麻烦你一直帮忙了
回复
pengzhistar 2010-09-28
楼主 你粗心了!你的类里面的方法是这样的

saveChartAsPNG(JFreeChart chart, int width,
int height, ChartRenderingInfo info, HttpSession session)

但是你在调用的时候JSP中

fileName = BarChart.saveChartAsPNG(chart, 800, 600, session);

少了一个参数!不会报错吗?
还有检查下你的方法中的那个路径是否正确,打印出来!
回复
Ginie 2010-09-27
[Quote=引用 14 楼 pengzhistar 的回复:]
你的jsp代码 fileName = BarChart.saveChartAsPNG(chart, 800, 600, session);这来改下,这个是保存图片的操作啊 !
<img src="<%=url%>" width="500" height="300">
这里改成: <%=request.getContextPath()+"/upload/"+fileName %>
[/Quote]

不行啊,我按照你说的改了,它还是不会在我指定的路径生成图片,还是在Tomcat的零时目录下。页面打印出来的路径是可以的,在根目录
<img src="/st2/upload/jfreechart-11106.png" width="500" height="300">
但是它却没在这里生成图片啊
回复
pengzhistar 2010-09-27
你的jsp代码 fileName = BarChart.saveChartAsPNG(chart, 800, 600, session);这来改下,这个是保存图片的操作啊 !
<img src="<%=url%>" width="500" height="300">
这里改成: <%=request.getContextPath()+"/upload/"+fileName %>
回复
pengzhistar 2010-09-27
楼主不是只要控制图片路径吗 ?生成图片的方法网上一大把!贴一段网站上复制的代码

double[] data = {85, 156, 179.5, 211, 123};
//The labels for the bar chart
String[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"};

DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < data.length; i++) {
dataset.addValue(data[i], null, labels[i]);
}

JFreeChart chart = ChartFactory.createBarChart3D("Weekly Server Load", "Work Week 25", "MBytes", dataset, PlotOrientation.VERTICAL, false, false, false);
chart.setBackgroundPaint(new Color(0xE1E1E1));

CategoryPlot plot = chart.getCategoryPlot();

// 设置Y轴显示整数
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

CategoryAxis domainAxis = plot.getDomainAxis();
//设置距离图片左端距离
domainAxis.setLowerMargin(0.05);

BarRenderer3D renderer = new BarRenderer3D();
//设置柱的颜色
renderer.setSeriesPaint(0, new Color(0xff00));
plot.setRenderer(renderer);

//这里是保存图片到路径:默认是保存到tomcat的临时文件夹,你用我刚刚保存方法!
//String filename = ServletUtilities.saveChartAsPNG(chart, 300, 280, null, session);


回复
Ginie 2010-09-27
[Quote=引用 10 楼 pengzhistar 的回复:]
Java code

package com.sunun.util.jfreechart;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import ……
[/Quote]

这个方法我用了,可还是没有用呢,我把我的代码改成

public static String saveChartAsPNG(JFreeChart chart, int width,
int height, ChartRenderingInfo info, HttpSession session)
throws IOException {
if (chart == null) {
throw new IllegalArgumentException("Null 'chart' argument.");
}

String prefix = ServletUtilities.getTempFilePrefix();
if (session == null) {
prefix = ServletUtilities.getTempOneTimeFilePrefix();
}
prefix = prefix.replaceAll("jfreechart", "chart");
File tempFile = File.createTempFile(prefix, ".png",
new File(ServletActionContext.getServletContext().getRealPath(
"/upload")));
ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
if (session != null) {
ServletUtilities.registerChartForDeletion(tempFile, session);
}
return tempFile.getName();
}


.JSP

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset"%>
<%@ page import="org.jfree.chart.ChartFactory"%>
<%@ page
import="org.jfree.chart.JFreeChart,org.jfree.chart.servlet.ServletUtilities,com.test.jfreechart.BarChart"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>
<s:form namespace="/">

<%
DefaultPieDataset dpd = new DefaultPieDataset();

dpd.setValue("管理人员", 25);
dpd.setValue("市场人员", 25);
dpd.setValue("开发人员", 45);
dpd.setValue("其他人员", 10);

JFreeChart chart = ChartFactory.createPieChart("某公司组织结构图", dpd,
true, false, false);

String fileName = "";


fileName = BarChart.saveChartAsPNG(chart, 800, 600, session);
System.out.println("fileName=" + fileName);

String url = request.getContextPath() + "/DisplayChart?filename="
+ fileName;

System.out.println("url =" + url);
%>

<img src="<%=url%>" width="500" height="300">
</s:form>
</body>
</html>



他还是在Tomcat目录下生成图片,而且页面还是不会显示啊,页面的SRC显示的是
<img src="/st2/DisplayChart?filename=jfreechart-54554.png" width="500" height="300">
回复
Ginie 2010-09-27
感觉楼上的有点复杂,呵呵,我是刚学这个,可以告诉我怎么样子先简单的实现图片显示么,然后再加深研究你的方法,麻烦了
回复
pengzhistar 2010-09-27

package com.sunun.util.jfreechart;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.servlet.ServletUtilities;

public class JfreeChartUtilities extends ServletUtilities {
public static String saveChartAsPNG(JFreeChart chart, int width,
int height, HttpServletRequest request) throws IOException {
if (chart == null) {
throw new IllegalArgumentException("Null 'chart' argument.");
}

String prefix = ServletUtilities.getTempFilePrefix();
if (request.getSession() == null) {
prefix = ServletUtilities.getTempOneTimeFilePrefix();
}
prefix = prefix.replaceAll("jfreechart", "chart");
File tempFile = File.createTempFile(prefix, ".jpeg", new File(request
.getRealPath("images/imgchart")));

ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height);
if (request.getSession() != null) {
ServletUtilities.registerChartForDeletion(tempFile, request
.getSession());
}
return tempFile.getName();
}
}



楼主试试我的吧 我在工程是这样写的!
调用方法:

JfreeChartUtilities.saveChartAsPNG(chart, 600, 400,
request);
回复
andesen 2010-09-27
没写好

StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
PrintWriter w = new PrintWriter(print);// 输出MAP信息
String filename = ServletUtilities.saveChartAsPNG(chart, 800, 300,info, session);


.jsp

String filename = new ChartUtil1().createLineImg(session,w);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
回复
andesen 2010-09-27
路径不是写死的 是在web.xml配置的

<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<!-- Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart</url-pattern>
</servlet-mapping>

.java

StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
PrintWriter w = new PrintWriter(print);// 输出MAP信息
String filename = ServletUtilities.saveChartAsPNG(chart, 800, 300,info, session);
.jsp
[code=HTML]
String filename = new ChartUtil1().createLineImg(session,w);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;

[/code]
回复
唉,学子一下。jfreechat功能确实很强大
回复
Ginie 2010-09-27
哪个大哥大姐帮帮忙呀
回复
Ginie 2010-09-27
[Quote=引用 3 楼 hzz1988 的回复:]
最好别生成图片,因为占用资源,直接通过流的写入比较好,而且可以实时更新。
[/Quote]

hzz1988这么说可以帮我下么,我不知道怎么用流生成,可以给我个实例不,谢谢了
回复
hzz1988 2010-09-27
最好别生成图片,因为占用资源,直接通过流的写入比较好,而且可以实时更新。
回复
simaa0106 2010-09-27
There is no Action mapped for namespace / and action name DisplayChart. - [unknown location]

在命名空间"/"下找不到这个DisplayChart这个action。
回复
Ginie 2010-09-27
来个人帮帮忙吧,拜托啦
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-09-27 02:27
社区公告
暂无公告