简体   繁体   English

使用非null InputStream创建后,Image为null

[英]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. 我测试InputStream是否为null,而不是。 I then create a new BufferedImage from the InputStream. 然后我从InputStream创建一个新的BufferedImage。 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: 我通过html表单上传.tif文件,如下所示:

<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 : 来自javadoc

If no registered ImageReader claims to be able to read the resulting stream, null is returned. 如果没有已注册的ImageReader声称能够读取生成的流,则返回null。

You InputStream probably does not exactly contain a readable image. 您的InputStream可能并不完全包含可读图像。

Ah! 啊! So.. The reason is that ImageIO.read() does not support .tif images. 所以..原因是ImageIO.read()不支持.tif图像。 I tried it with a .jpg and it worked fine. 我尝试用.jpg,它工作正常。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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