我不知道ChartUtilities.saveChartAsJPEG()有什么用?
是不是他的第一个参数要求文件必须先存在?
如果是的话你需要先建好文件:
String fileName="../../../../../defaultroot/images/"+toller+".jpeg";
try{
File file= new File(fileName);
if(file.createNewFile())
  ChartUtilities.saveChartAsJPEG(new File(fileName),100,chart,600,600);
}
else 
  System.out.print("....Cant’t Create image File");
}
catch(IOException ex){
System.out.print("....Cant’t Create image File");
}如果javabean是使用jsp中传进来的值就用
request.getRealPath()来获得决对路径

解决方案 »

  1.   

    可以做成一个变量
    用jsp把地址传过来
      

  2.   

    不是要绝对路径啦。
    就是要在程序中运用相对路径。
    文件的树结构上面已给出了。
    简单来说,就是用new File(filepath),这是java的io包里有的。一般我的filepath都是用绝对路径,但这次编程需要,要用相对路径。文件树结构第一帖已给出,不知这个相对路径该怎么写。
      

  3.   

    如果按照你所说的文件树结构来表示的话
    String fileName="../../../../../defaultroot/images/"+toller+".jpeg";
    显然是错误的,多了一个"../",相对目录是从“XXX.java”的上级目录开始的
    也就是business了
      

  4.   

    呵呵,是多了一个../,但我试了一下,减小了一个../也不行。:)HELP!!!!!!
      

  5.   

    如果ChartUtilities.saveChartAsJPEG(new File(fileName),100,chart,600,600)中的
    第一个参数new File(fileName)要求fileName必须先存在的话,你还要先
    file.createNewFile()把fileName文件建好,具体如何做请参考我发的第一贴,
    如果还不行,我实再找不出错误来了中,呵呵我按你的目录结构完全可以生成图像文件!
    d:/hbzf/src/com/hbzf/business/Test.java
    d:/hbzf/defaultroot/images/import java.io.*;
    public class Test
    {  
       public static void main(String[] args)
       {
          try
          {  
             File pathName = new File("../../../../defaultroot/images/toller.jpeg");
             System.out.println(pathName.getCanonicalPath());
     System.out.println(pathName.createNewFile());
      }
          catch(IOException e)
          {  
             e.printStackTrace(); 
          }
       }
    }
      

  6.   

    关注ing,我写帖子问了2个星期了,还不知道确切答案!
      

  7.   

    如果你用jsp/servlet下用JFreeChart,可以传一个HttpServletRequest request对象进去,用request.getServerName()+":"+request.getServerPort()取得服务器名称,然后添加相对路径就行了,用new url()来生成图像数据
      

  8.   

    问题解决了,但莫名其妙~~~~~~~
    这是我的程序,大家可以看一下。
    package com.scut.hbzf.business.st;
    import org.jfree.chart.*;
    import org.jfree.data.*;
    import org.jfree.chart.axis.*;
    import org.jfree.chart.plot.*;
    import org.jfree.chart.urls.*;
    import java.awt.*;
    import java.math.*;
    import java.io.*;
    import java.util.*;
    import java.io.*;
    public class PieChart {
    String potName;
    int potValue;
    public static String  drawPie(String title,ArrayList array,String toller){
    String DrawStr;
    Iterator it=array.iterator();
    DefaultPieDataset pieData=new DefaultPieDataset();
    while(it.hasNext()){
    PieChart pieDraw=(PieChart)it.next();
    pieData.setValue(pieDraw.potName,pieDraw.potValue);}//while(it.hasNext())JFreeChart chart=ChartFactory.createPie3DChart(title,pieData,true,true,true);
    chart.setTitle(new TextTitle(title,new Font("隶书",Font.ITALIC ,15)));
    chart.setBackgroundPaint(Color.blue);
    PiePlot pie=(PiePlot)chart.getPlot();
    pie.setSectionLabelType(PiePlot.NAME_AND_PERCENT_LABELS);
    pie.setPercentFormatString("#,###0.0#%");
    pie.setBackgroundPaint(Color.white);
    pie.setSectionLabelFont(new Font("黑体",Font.TRUETYPE_FONT,12));
    pie.setBackgroundAlpha(0.6f);
    pie.setForegroundAlpha(0.9f);
    String fileName="./defaultroot/images/"+toller+".jpeg";//为什么会这样啊???:|
    DrawStr="<img src="+fileName+"border=0>";
    try{
    ChartUtilities.saveChartAsJPEG(new File(fileName),100,chart,600,600);
    }
    catch(IOException ex){
    System.out.print("....Cant’t Create image File");
    }return DrawStr;
    }public static void main(String[] args){
    ArrayList hh=new ArrayList();
    PieChart bb;
    bb=new PieChart();
    bb.potName="许耿聪";
    bb.potValue=34;
    hh.add(bb);
    bb=new PieChart();
    bb.potName="李静锴";
    bb.potValue=26;
    hh.add(bb);
    bb=new PieChart();
    bb.potName="陈健";
    bb.potValue=46;
    hh.add(bb);
    String title="同学";
    String toller="xgc";
    String dd=drawPie(title,hh,toller);
    }}
    这个问题迟点可能还要麻烦大家,好了。给分了。:P
      

  9.   

    如果我没想错的话你的文件目录结构可能类是这样:
    d:\hbzf\com\scut\hbzf\business\st\XXX.java;
    d:\hbzf\defaultroot\images\XXX.jpeg;
    classpath中包含"d:\hbzf"
    dos窗口的当前路径是:
    d:\hbzf>
    当你new File(fileName)时当前路径就是d:\hbzf,
    new File("./defaultroot/images/XXX.jpeg")
    也就是new File("d:/hbzf/defaultroot/images/XXX.jpeg")