ImageIO Can't create output stream!

java在生成验证码时,经常要用到ImageIO类,今天在一台windows 2008 server上部署好项目之后,项目怎么都刷不出来验证码,后台可以捕捉到的异常,里面包含有Can’t create output stream!

初步调查

在我开始写这篇解决问题的办法时,我还没有解决问题。

  1. 项目已经成功运行了很多个版本,在windows server 2003上运行OK。
  2. 在windows7上运行也OK。

但偏偏到了windows server 2008上却拉不出来验证码,真是引起了我极大的兴趣!

主要的异常信息如下:

ERROR 2015-11-25 10:25:44,061 com.honzh.socket.server.communicate.biz.CodeBiz: Can‘t create output stream!
javax.imageio.IIOException: Can‘t create output stream!
    at javax.imageio.ImageIO.write(Unknown Source)

Caused by: javax.imageio.IIOException: Can‘t create cache file!
    at javax.imageio.ImageIO.createImageOutputStream(Unknown Source)
    ... 11 more
Caused by: java.io.IOException: 系统找不到指定的路径。
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.checkAndCreate(Unknown Source)
    at java.io.File.createTempFile0(Unknown Source)
    at java.io.File.access$100(Unknown Source)
    at java.io.File$1.createTempFile(Unknown Source)
    at sun.misc.IOUtils.createTempFile(Unknown Source)
    at javax.imageio.stream.FileCacheImageOutputStream.<init>(Unknown Source)
    at com.sun.imageio.spi.OutputStreamImageOutputStreamSpi.createOutputStreamInstance(Unknown Source)
    ... 12 more

透过这个异常信息,我开始追本溯源,当然就是翻看源码了。

先看ImageIO.write内容,可以定位到是createImageOutputStream抛出了IIOException异常。

    public static boolean write(RenderedImage im,
                                String formatName,
                                OutputStream output) throws IOException {
        if (output == null) {
            throw new IllegalArgumentException("output == null!");
        }
        ImageOutputStream stream = null;
        try {
            stream = createImageOutputStream(output);
        } catch (IOException e) {
            throw new IIOException("Can‘t create output stream!", e);
        }

        boolean val;
        try {
            val = write(im, formatName, stream);
        } finally {
            stream.close();
        }
        return val;
    }

再看createImageOutputStream方法,可以定位到ImageOutputStreamSpi类的createOutputStreamInstance方法

                try {
                    return spi.createOutputStreamInstance(output,
                                                          usecache,
                                                          getCacheDirectory());
                } catch (IOException e) {
                    throw new IIOException("Can‘t create cache file!", e);
                }

然后,我们定位到OutputStreamImageOutputStreamSpi的createOutputStreamInstance方法,OutputStreamImageOutputStreamSpi继承了ImageOutputStreamSpi类

    public ImageOutputStream createOutputStreamInstance(Object output,
                                                        boolean useCache,
                                                        File cacheDir)
        throws IOException {
        if (output instanceof OutputStream) {
            OutputStream os = (OutputStream)output;
            if (useCache) {
                return new FileCacheImageOutputStream(os, cacheDir);
            } else {
                return new MemoryCacheImageOutputStream(os);
            }
        } else {
            throw new IllegalArgumentException();
        }
    }

OK,关键的地方来了,我们继续挖,直到挖到FileCacheImageOutputStream构造方法

    public FileCacheImageOutputStream(OutputStream stream, File cacheDir)
        throws IOException {
        if (stream == null) {
            throw new IllegalArgumentException("stream == null!");
        }
        if ((cacheDir != null) && !(cacheDir.isDirectory())) {
            throw new IllegalArgumentException("Not a directory!");
        }
        this.stream = stream;
        this.cacheFile =
            sun.misc.IOUtils.createTempFile("imageio", ".tmp", cacheDir);
        this.cache = new RandomAccessFile(cacheFile, "rw");

        this.closeAction = StreamCloser.createCloseAction(this);
        StreamCloser.addToQueue(closeAction);
    }

到这里,我想你需要看一看该方法的javadoc了。

Constructs a FileCacheImageOutputStream that will write to a given outputStream.

A temporary file is used as a cache. If cacheDiris non-null and is a directory, the file will be created there. If it is null, the system-dependent default temporary-file directory will be used (see the documentation for File.createTempFile for details).

让我们去看File.createTempFile方法,这时候就需要上java api帮助文档了!

createTempFile

public static File createTempFile(String prefix,

String suffix,

File directory)

throws IOException在指定目录中创建一个新的空文件,使用给定的前缀和后缀字符串生成其名称。如果此方法成功返回,则可以保证:

由返回的抽象路径名表示的文件在此方法被调用之前不存在。

此方法及其所有变体都不会在虚拟机的当前调用中再次返回相同的抽象路径名。

此方法只提供了临时文件的部分功能。要安排自动删除此方法创建的文件,可使用 deleteOnExit() 方法。

prefix 参数至少必须是三个字节长。建议前缀使用一个短的、有意义的字符串,比如 “hjb” 或 “mail”。suffix 参数可以为 null,在这种情况下,将使用后缀 “.tmp”。

要创建新文件,可能首先要调整前缀和后缀,使其满足底层平台的限制。如果前缀太长,则将它截断,但前三个字符将始终保留。如果后缀太长,则将它截断,但如果它以句点字符 (‘.’) 开始,则该句点以及后跟的前三个字符将始终保留。进行了这些调整后,通过连接前缀、五个或更多个内部生成的字符以及后缀,便生成了新文件的名称。

如果 directory 参数为 null,则使用与系统有关的默认临时文件目录。默认临时文件目录由系统属性 java.io.tmpdir 指定。在 UNIX 系统上,此属性的默认值通常是 “/tmp” 或 “/var/tmp”;在 Microsoft Windows 系统上,该值通常是 “C:\WINNT\TEMP”。在调用 Java 虚拟机时,可为此系统属性提供不同的值,但不保证使用程序更改此属性会对此方法使用的临时目录产生影响。

参数:

prefix - 用于生成文件名的前缀字符串;必须至少是三字符长

suffix - 用于生成文件名的后缀字符串;可以为 null,在这种情况下,将使用后缀 “.tmp”

directory - 将创建的文件所在的目录;如果使用默认临时文件目录,则该参数为 null

返回:

表示新建空文件的抽象路径名

抛出:

IllegalArgumentException - 如果 prefix 参数包含的字符少于三个

IOException - 如果无法创建文件

SecurityException - 如果存在安全管理器,且其 SecurityManager.checkWrite(java.lang.String) 方法不允许创建文件

注意,这里告诉我们去看一下windows的C:\WINNT\TEMP目录。

WINNT是啥玩意,我反正是不太清楚,问问度娘:

Microsoft Windows NT(New Technology)是Microsoft在1993年推出的面向工作站、网络服务器和大型计算机的网络操作系统,也可做PC操作系统。它与通信服务紧密集成,基于OS/2 NT基础编制。OS/2由微软和IBM联合研制,分为微软的Microsoft OS/2 NT与IBM的IBM OS/2。协作后来不欢而散,IBM继续向市场提供先前的OS/2版本,微软则把自己的OS/2 NT的名称改为Windows NT,即第一代的Windows NT 3.1。

大概可能是以上的意思。

然后,我对比了一下win7和windows server 2008的

很遗憾,没有找到我想要的,不高兴!

继续探索

再回过头来看看,发现这句关键字:

默认临时文件目录由系统属性 java.io.tmpdir 指定

写个程序测试下

public class Test {

    public static void main(String[] args) {
        System.out.println(System.getProperty("java.io.tmpdir"));

    }

}
系统 输出
win7 C:\Users\abc\AppData\Local\Temp\
server 2008 C:\Users\ADMINI~1\AppData\Local\Temp\2\

顺着目录找下来,windows 2008的大概目录应该是C:\Users\Administrator\AppData\Local,但是也找不下去,没有找到2。

先新建一个2目录试试,结果发现验证码可以输出了!


王二语录

那么人生何处不爬虫,爬虫请标http://blog.csdn.net/qing_gee

小问题,大经验!

ImageIO Can't create output stream!

时间: 2024-08-15 06:53:06

ImageIO Can't create output stream!的相关文章

解决tomcat报错javax.imageio.IIOException: Can&#39;t create output stream!

启动tomcat catalina.out报错如下,登陆的时候无法显示验证码 2017-06-09 11:23:06,628 DEBUG org.springframework.web.servlet.DispatcherServlet 845 - DispatcherServlet with name 'spring-mvc' processing GET request for [/data-analysis/kaptchaImage.do]2017-06-09 11:23:06,629 D

MessagePack Java Jackson 在不关闭输出流(output stream)的情况下序列化多变量

com.fasterxml.jackson.databind.ObjectMapper 在默认的情况下在写出输入后将会关闭输出流(output stream). 如果你希望序列化多值变量在同一个输出流的情况下,你不希望在输出完一个就关闭输出流,你可以设置  JsonGenerator.Feature.AUTO_CLOSE_TARGET 参数为 False. 本测试方法,可以在 https://github.com/cwiki-us-demo/serialize-deserialize-demo-

将andriod应用安装为系统应用

package com.example.bnoainstaller; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import

点击下载按钮下载文件到本地 download stream to local

protected void Down_Load(object sender, EventArgs e) { //create a stream for the file Stream stream = null; //this controls how many bytes to read at a time and send to the client int bytesToRead = 10000; //buffer to read bytes in chunk size specifie

Akka Stream文档翻译:Quick Start Guide: Reactive Tweets

Quick Start Guide: Reactive Tweets 快速入门指南: Reactive Tweets (reactive tweets 大概可以理解为“响应式推文”,在此可以测试下GFW是否还在正常工作 Twitter) A typical use case for stream processing is consuming a live stream of data that we want to extract or aggregate some other data fr

Stream Processing 101: From SQL to Streaming SQL in 10 Minutes

原文:https://wso2.com/library/articles/2018/02/stream-processing-101-from-sql-to-streaming-sql-in-ten-minutes/ We have entered an era where competitive advantage comes from analyzing, understanding, and responding to an organization’s data. When doing

【转】Scala 中的 Stream

///////////////////////////////////// def numsFrom(n: Int): Stream[Int] = n #:: numsFrom(n + 1) def testStream = { val tenOrMore = numsFrom(10) println(tenOrMore) println(tenOrMore.tail) println(tenOrMore.tail.tail) println(tenOrMore.tail.tail.tail)

Input,output,and files【C++】

a c++ program views input or output as a stream of bytes.view..as把..看作是on input,a program extracts bytes from an input stream,and on output,a program inserts bytes into the output stream.extract:提取字节.. //输入:程序从输入流中提取:输出:插入字节到输出流:for a text-oriented p

流(stream)

流的概念 Java程序通过流来完成输入/输出.流是生产或消费信息的抽象.流通过Java的输入/输出系统与物理设备链接.尽管与它们链接的物理设备不尽相同,所有流的行为具有同样的方式.这样,相同的输入/输出类和方法适用于所有类型的外部设备.这意味着一个输入流能够抽象多种不同类型的输入:从磁盘文件,从键盘或从网络套接字.同样,一个输出流可以输出到控制台,磁盘文件或相连的网络.流是处理输入/输出的一个洁净的方法,例如它不需要代码理解键盘和网络的不同.Java中流的实现是在java.io包定义的类层次结构