图片生成失败, Can't create output stream!

遇到一个奇葩问题,分析报告的图片在测试环境上不显示,但在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”。图片生成失败,自然就无法正常显示。
以下是文档中附上的源码。
  1. private static boolean hasCachePermission() {  
  2.     Boolean hasPermission = getCacheInfo().getHasPermission();  
  3.   
  4.     if (hasPermission != null) {  
  5.         return hasPermission.booleanValue();  
  6.     } else {  
  7.         try {  
  8.             SecurityManager security = System.getSecurityManager();  
  9.             if (security != null) {  
  10.                 File cachedir = getCacheDirectory();  
  11.                 String cachepath;  
  12.   
  13.                 if (cachedir != null) {  
  14.                     cachepath = cachedir.getPath();  
  15.                 } else {  
  16.                     cachepath = getTempDir();  
  17.   
  18.                     if (cachepath == null) {  
  19.                         getCacheInfo().setHasPermission(Boolean.FALSE);  
  20.                         return false;  
  21.                     }  
  22.                 }  
  23.   
  24.                 security.checkWrite(cachepath);  
  25.             }  
  26.         } catch (SecurityException e) {  
  27.             getCacheInfo().setHasPermission(Boolean.FALSE);  
  28.             return false;  
  29.         }  
  30.   
  31.         getCacheInfo().setHasPermission(Boolean.TRUE);  
  32.         return true;  
  33.     }  
  34. }  
 
  1. /** 
  2.   * Returns the default temporary (cache) directory as defined by the 
  3.   * java.io.tmpdir system property. 
  4.   */  
  5.  private static String getTempDir() {  
  6.      GetPropertyAction a = new GetPropertyAction("java.io.tmpdir");  
  7.      return (String)AccessController.doPrivileged(a);  
  8.  }  
 tomcat 启动的时候显示
  1. Using CATALINA_BASE:   /home/appuser/appservers/tomcat-feilong  
  2. Using CATALINA_HOME:   /home/appuser/appservers/tomcat-feilong  
  3. Using CATALINA_TMPDIR: /home/appuser/appservers/tomcat-feilong/temp  
  4. Using JRE_HOME:        /usr/lib/jvm/java-6-sun  
  5. Using CLASSPATH:       /home/appuser/appservers/tomcat-feilong/bin/bootstrap.jar  
 



 

 



 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值