简体   繁体   中英

Image is null after creation with not null InputStream

I have the following code:

public BufferedImage inputStreamToImage (InputStream stream) {        

    BufferedImage image = null;

    if (stream != null ) {

        try {
            image = ImageIO.read(stream);
            // image is still null here <--
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    return image;
}

I test if the InputStream is null, which it's not. I then create a new BufferedImage from the InputStream. Again nothing goes wrong and code executes like it's working. But then when I try and use the image it's null.. In other words, right after a "successful" creation I test the image and it's null.

EDIT

I upload a .tif file via a html form like so:

<form action="webresources/read" method="post" enctype="multipart/form-data">

    <p>
    Select a file : <input type="file" name="file" id="file" />
    </p>

    <input type="submit" value="Upload It" />

</form>

Any help? Thanks in advance :)

From the javadoc :

If no registered ImageReader claims to be able to read the resulting stream, null is returned.

You InputStream probably does not exactly contain a readable image.

Ah! So.. The reason is that ImageIO.read() does not support .tif images. I tried it with a .jpg and it worked fine.

是否有可能在ImageIO的文档中声明以下情况 - “InputStream包含在ImageInputStream中。如果没有注册的ImageReader声称能够读取结果流,则返回null。”?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM