遇到一个奇葩问题,分析报告的图片在测试环境上不显示,但在alicloud和本机上是正常的。
分析了下, 首先将 ChartUtilities.writeChartAsPNG()改为ChartUtilities.writeChartAsJEPG();把图片格式改为jepg,发现图片还是显示不了。
连上远程桌面,发现生成图片的时候后台报错:
Can't create output stream。
查找发现也有人遇到同样的问题,看完文档http://feitianbenyue.iteye.com/blog/1743281。
终于找到原因,生成图片的时候要用到io流,会读取变量java.io.tmpdir!如果没有自定义路径,默认读取路径是tomcat下的temp文件夹。
当temp文件夹不存在时,就会出现”Can't create output stream”。图片生成失败,自然就无法正常显示。
以下是文档中附上的源码。
- private static boolean hasCachePermission() {
- Boolean hasPermission = getCacheInfo().getHasPermission();
- if (hasPermission != null) {
- return hasPermission.booleanValue();
- } else {
- try {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- File cachedir = getCacheDirectory();
- String cachepath;
- if (cachedir != null) {
- cachepath = cachedir.getPath();
- } else {
- cachepath = getTempDir();
- if (cachepath == null) {
- getCacheInfo().setHasPermission(Boolean.FALSE);
- return false;
- }
- }
- security.checkWrite(cachepath);
- }
- } catch (SecurityException e) {
- getCacheInfo().setHasPermission(Boolean.FALSE);
- return false;
- }
- getCacheInfo().setHasPermission(Boolean.TRUE);
- return true;
- }
- }
- /**
- * Returns the default temporary (cache) directory as defined by the
- * java.io.tmpdir system property.
- */
- private static String getTempDir() {
- GetPropertyAction a = new GetPropertyAction("java.io.tmpdir");
- return (String)AccessController.doPrivileged(a);
- }
tomcat 启动的时候显示
- Using CATALINA_BASE: /home/appuser/appservers/tomcat-feilong
- Using CATALINA_HOME: /home/appuser/appservers/tomcat-feilong
- Using CATALINA_TMPDIR: /home/appuser/appservers/tomcat-feilong/temp
- Using JRE_HOME: /usr/lib/jvm/java-6-sun
- Using CLASSPATH: /home/appuser/appservers/tomcat-feilong/bin/bootstrap.jar