黑马程序员技术交流社区
标题: 【广州校区】+验证码程序报错javax.imageio.IIOException [打印本页]
作者: 灵熙云 时间: 2018-8-7 15:25
标题: 【广州校区】+验证码程序报错javax.imageio.IIOException
异常代码:[Java] 纯文本查看 复制代码
javax.imageio.IIOException:<strong> Can't create output stream!</strong>
javax.imageio.ImageIO.write(ImageIO.java:1560)
com.jumbo.brandstore.util.SecurityUtil.generateSecurityImage(SecurityUtil.java:90)
com.jumbo.brandstore.web.servlet.SecurityImageServlet.service(SecurityImageServlet.java:57)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.imageio.IIOException:<strong> Can't create cache file!</strong>
javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:397)
javax.imageio.ImageIO.write(ImageIO.java:1558)
com.jumbo.brandstore.util.SecurityUtil.generateSecurityImage(SecurityUtil.java:90)
com.jumbo.brandstore.web.servlet.SecurityImageServlet.service(SecurityImageServlet.java:57)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.io.IOException: <strong>No such file or directory</strong>
java.io.UnixFileSystem.createFileExclusively(Native Method)
java.io.File.checkAndCreate(File.java:1704)
java.io.File.createTempFile(File.java:1792)
javax.imageio.stream.FileCacheImageOutputStream.<init>(FileCacheImageOutputStream.java:71)
com.sun.imageio.spi.OutputStreamImageOutputStreamSpi.createOutputStreamInstance(OutputStreamImageOutputStreamSpi.java:50)
javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:393)
javax.imageio.ImageIO.write(ImageIO.java:1558)
com.jumbo.brandstore.util.SecurityUtil.generateSecurityImage(SecurityUtil.java:90)
com.jumbo.brandstore.web.servlet.SecurityImageServlet.service(SecurityImageServlet.java:57)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
解决方法:1.修改验证码程序 把
ImageIO.write(image, "jpeg", response.getOutputStream());
修改为
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(response.getOutputStream());
encoder.encode(image);
注意:jsp上要增加com.sun.image.codec.jpeg.JPEGCodec,com.sun.image.codec.jpeg.JPEGImageEncoder这些类的import
2.tomcat 的temp 目录必须有为什么ImageIO.write 这种方式需要 temp目录呢?看看源码:
[Java] 纯文本查看 复制代码
public static ImageOutputStream <strong>createImageOutputStream</strong>(Object output)
throws IOException {
if (output == null) {
throw new IllegalArgumentException("output == null!");
}
Iterator iter;
// Ensure category is present
try {
iter = theRegistry.getServiceProviders(ImageOutputStreamSpi.class,
true);
} catch (IllegalArgumentException e) {
return null;
}
boolean usecache =<strong> getUseCache() && hasCachePermission()</strong>;
while (iter.hasNext()) {
ImageOutputStreamSpi spi = (ImageOutputStreamSpi)iter.next();
if (spi.getOutputClass().isInstance(output)) {
try {
return spi.createOutputStreamInstance(output,
usecache,
<strong>getCacheDirectory()</strong>);
} catch (IOException e) {
throw new IIOException("Can't create cache file!", e);
}
}
}
return null;
}
会调用cache 文件夹 ,他所谓的cache 文件夹是怎么样定义的呢?
[Java] 纯文本查看 复制代码
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;
}
}
写得也很清楚,如果自己设置了 setCacheDirectory 那么会使用自定义的,否则调用getTempDir()
[Java] 纯文本查看 复制代码
/**
* 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);
}
Oh, 会读取变量java.io.tmpdir!
还记得 tomcat 启动的时候,会显示
[Java] 纯文本查看 复制代码
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
而tomcat启动的时候,调用jvm会设置 java.io.tmpdir 参数
[Java] 纯文本查看 复制代码
rem CATALINA_TMPDIR (Optional) Directory path location of temporary directory
rem the JVM should use (java.io.tmpdir). Defaults to
rem %CATALINA_BASE%\temp.
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -
Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -
Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -
Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
这下终于弄明白了, ImageIO.write(image, "JPEG", os) 这种方式来生成图片、验证码 tomcat必须要有temp文件夹
手动创建temp目录,问题解决!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |