日期:2014-05-18  浏览次数:20601 次

jfreeChart路径问题
按照网上的说法改了saveChartAsPNG()方法,写了个BarChart类,内容如下
Java code

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页面中这样写代码的
Java code

<%@ 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);