感谢您的回复。
自从你给了我,我一直在尝试发短信。从某种意义上说仍然不起作用:现在不再抛出该异常,而是出于某种原因创建了空白图像。
所以我从这个出发:
public BarChartGenerator(org.qcri.power.ws.client.Server oServer, ServerStatistics oServerStat, List lVMs, String path) extends ApplicationFrame {
System.setProperty("java.awt.headless", "true");
boolean headless = GraphicsEnvironment.isHeadless();
System.out.println("Headless: " + headless);
Toolkit tk = Toolkit.getDefaultToolkit();
tk.beep();
// generate dataset
final CategoryDataset dataset = createDataset(lVMs);
setChart(createChart(oServer, oServerStat, lVMs, dataset));
// create PNG of the chart...
setFilename("chart_server"+oServer.getHost()+"_"+System.currentTimeMillis()+".gif");
File fImageFile = new File(path+filename);
try {
ChartUtilities.saveChartAsPNG(fImageFile, chart, PowerInterface.CHART_WIDTH, PowerInterface.CHART_HEIGHT);
} catch (Exception e) {
e.printStackTrace();
}
}
到以下(摆脱extends ApplicationFrame
)并使用你给我的例子:
public BarChartGenerator(org.qcri.power.ws.client.Server oServer, ServerStatistics oServerStat, List lVMs, String path) {
System.setProperty("java.awt.headless", "true");
boolean headless = GraphicsEnvironment.isHeadless();
System.out.println("Headless: " + headless);
Toolkit tk = Toolkit.getDefaultToolkit();
tk.beep();
// generate dataset
final CategoryDataset dataset = createDataset(lVMs);
setChart(createChart(oServer, oServerStat, lVMs, dataset));
// create PNG of the chart...
setFilename("chart_server"+oServer.getHost()+"_"+System.currentTimeMillis()+".gif");
try {
BufferedImage bufferedImage = getChart().createBufferedImage(PowerInterface.CHART_WIDTH, PowerInterface.CHART_HEIGHT);
ImageIO.write(bufferedImage, "gif", new FileOutputStream(path+getFilename()));
} catch (Exception e) {
e.printStackTrace();
}
}
在日志中它抱怨ImageIO.write(bufferedImage, "gif", new FileOutputStream(path+getFilename()));
给出这个错误(当文件在那里时!):
javax.imageio.IIOException: Can't create output stream!
at javax.imageio.ImageIO.write(ImageIO.java:1560)
at org.qcri.power.util.BarChartGenerator.<init>(BarChartGenerator.java:106)
有什么线索吗?