ImageIO Can ' t create output stream!

Source: Internet
Author: User
Tags truncated


Java in the generation of verification code, often use the ImageIO class, today, after deploying a project on a Windows server, the project will not be able to brush out the verification code, the background can catch the exception, which contains can ' t create output stream!

Preliminary investigation


I didn't solve the problem when I started to write a solution to the problem.


    1. The project has successfully run many versions and runs OK on Windows Server 2003.
    2. Running on the windows7 is OK.


But it happened to Windows Server 2008 but not pull out the verification code, it really aroused my great interest!



The main exception information is as follows:


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: the system could not find the specified path.
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 


Through this abnormal information, I began to trace the source, of course, is to look over.



First look at the mageio.write content, you can locate the Createimageoutputstream throws a Iioexception exception.


 
 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;
    }


Looking at the Createimageoutputstream method, we can locate the Createoutputstreaminstance method of the Imageoutputstreamspi class.



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


Then we navigate to the Createoutputstreaminstance method of the Outputstreamimageoutputstreamspi,  Outputstreamimageoutputstreamspi inherited the Imageoutputstreamspi class


 
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, the key place came, and we continued digging until we dug into the filecacheimageoutputstream construction method .


 
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);
    }


Here, I think you need to take a look at the Javadoc of the method.


Constructs a filecacheimageoutputstream that would write to a given outputstream.

A temporary file is used as a cache. If Cachediris Non-null and is a directory, the file would be created there. If It is null, the system-dependent default Temporary-file directory would be used (see the documentation for  File.crea Tetempfile for details).


Let's go to the File.createtempfile method and we need the Java API help documentation!



Createtempfile
public static File createtempfile (String prefix,
String suffix,
File directory)
Throws IOException creates a new empty file in the specified directory, using the given prefix and suffix string to generate its name. If this method returns successfully, you can guarantee:



The file represented by the returned abstract path name does not exist until this method is called.
Neither this method nor any of its variants returns the same abstract pathname again in the current call of the virtual machine.
This method only provides some of the functionality of the temporary file. To schedule files created by this method to be automatically deleted, use the Deleteonexit () method.
The prefix parameter must be at least three bytes long. It is recommended that the prefix use a short, meaningful string, such as "HJB" or "mail". The suffix parameter can be null, in which case the suffix ". tmp" is used.



To create a new file, you may want to first adjust the prefix and suffix so that it meets the limitations of the underlying platform. If the prefix is too long, it is truncated, but the first three characters are always preserved. If the suffix is too long, it is truncated, but if it starts with a period character ('. '), the period and the first three characters followed are always preserved. With these adjustments, the name of the new file is generated by connecting the prefix, five or more internally generated characters, and the suffix.



If the directory parameter is NULL, the system-related default temp file directory is used. The default temporary file directory is specified by the system property Java.io.tmpdir. On UNIX systems, the default value for this property is usually "/tmp" or "/var/tmp"; on Microsoft Windows systems, this value is typically "C:\WINNT\TEMP". When calling a Java virtual machine, you can provide different values for this system property, but there is no guarantee that using a program to change this property will have an impact on the temporary directory used by this method.



Parameters:
Prefix-the prefix string used to generate the file name; must be at least three characters in length
suffix-the suffix string used to generate the file name, or nullable, in which case the suffix ". tmp" will be used
Directory-the folder where the file will be created, or null if the default temp file directory is used
Return:
Abstract path name representing new empty file
Thrown:
IllegalArgumentException-If the prefix parameter contains fewer than three characters
IOException-If the file cannot be created
SecurityException-If a security manager is present and its securitymanager.checkwrite (java.lang.String) method does not allow file creation



Note that this tells us to look at the Windows C:\WINNT\TEMP directory.



Winnt is what thing, I was not very clear, ask degrees Niang:


Microsoft Windows NT (New Technology) is a network operating system for workstations, Web servers, and mainframe computers that Microsoft introduced in 1993, as well as a PC operating system. It is tightly integrated with communication services and is based on the OS/2 NT Foundation. OS/2 is jointly developed by Microsoft and IBM and is divided into Microsoft OS/2 NT and IBM IBM OS/2. The collaboration later broke down, IBM continued to provide the market with the previous version of OS/2, and Microsoft changed its OS/2 NT name to Windows NT, the first generation of Windows NT 3.1.


Probably the meaning of the above.



Then I compared the Win7 and Windows Server 2008



Unfortunately, did not find what I want, not happy!


Continue to explore


Look back and find this keyword:

The default temp file directory is specified by the System Properties Java.io.tmpdir

Write a program to test the



 
 
public class Test {

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

    }

}
system Output
Win7 C:\Users\abc\AppData\Local\Temp\
Server 2008 C:\users\admini~1\appdata\local\temp\2\





Looking down the directory, the directory of Windows 2008 should be C:\Users\Administrator\AppData\Local, but can not find, not found 2.



First create a new 2 directory to try, the results found that the verification code can be output!


Quotations from Wang ER

Then life where not reptile, crawler please mark Http://blog.csdn.net/qing_gee
Small problem, big experience!


ImageIO Can ' t create output stream!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.