目录

异常代码:

解决方法:

1.修改验证码程序 

2.tomcat 的temp 目录必须有


异常代码:

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目录呢?看看源码:   

 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 文件夹是怎么样定义的呢?   

 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()

   /**

     * 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 启动的时候,会显示

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  参数 

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目录,问题解决!

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐