ImageIO.getImageWriter(reader) not working inside tomcat
See original GitHub issueHello Harald,
I wrote a basic test code that does work in a standalone test class:
public class Test {
public static void main(String[] arg)
{
try
{
ImageInputStream iis = ImageIO.createImageInputStream(new FileInputStream(arg[0]));
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (!readers.hasNext())
throw new IOException("can't decode this image inputstream");
ImageReader reader = readers.next();
ImageReadParam param = reader.getDefaultReadParam();
reader.setInput(iis, true, true);
BufferedImage img = reader.read(0, param);
ImageWriter writer = ImageIO.getImageWriter(reader);
if (writer == null)
throw new IOException("can't find writer, reader provider: " + reader.getOriginatingProvider());
System.out.println("Writer: " + writer.getClass());
}
catch (Throwable t)
{
t.printStackTrace();
}
}
}
The same code doesn’t work inside a tomcat webapp, writer is null so an exception is thrown.
I have:
- added the listener as described in the guide
- fixed class name typos inside JPEGImageWriterSpi (class names were not starting with “com”
I have verified with a test loop that the ImageWriter is loaded:
static
{
System.out.println("ImageIO, looking for writers");
Iterator<ImageWriter> ite = ImageIO.getImageWritersByFormatName("jpeg");
while (ite.hasNext())
System.out.println("ImageIO writer: " + ite.next().getClass());
}
And the result:
logs/catalina.out:ImageIO, looking for writers logs/catalina.out:ImageIO writer: class com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageWriter logs/catalina.out:ImageIO writer: class com.sun.imageio.plugins.jpeg.JPEGImageWriter
Any idea about why is not working just inside tomcat? How to debug further?
Thanks,
Daniele.
Issue Analytics
- State:
- Created 10 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Java ImageIO suddenly stops working on Tomcat 7
We have a servlet running under tomcat 7 that converts stored TIFF images into JPEGs for display in a web application using the...
Read more >ImageIO (Java Platform SE 7 ) - Oracle Help Center
This method is provided principally for symmetry with getImageWriter(ImageReader) . Note that this method returns the "preferred" reader, which is the first in...
Read more >FAQ / Known Issues - Apache Software Foundation
I'm using the Java ImageIO to dynamically serve images and get strange Exceptions from time to time. Is this a bug in Tomcat?...
Read more >Java Examples for javax.imageio.spi.ImageWriterSpi
WithWriter getImageWriter(final Tile tile, final RenderedImage image, ... 2) (to be run if the above strategy did not found a suitable writer), ...
Read more >ImageIO (GNU Classpath 0.95 Documentation)
getImageWriter (ImageReader reader): Retrieve an image writer corresponding to an image reader, or null if reader is not registered or if no corresponding ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If, like me, you are still stumbling upon this old issue, and wonder what the best fix/workaround is these days:
com.twelvemonkeys.servlet.image.IIOProviderContextListener
(that is documented on the https://haraldk.github.io/TwelveMonkeys/ front page under “Deploying the plugins in a web app”).web.xml
file, you want to add the following line to theonStartup
method in yourSpringBootServletInitializer
subclass:of course with the following import:
With this, the standard ImageIO classes just work and you do not have to manually load any classes.
I hope this helps.
It would be great if @kkofler 's solution could be added to the documentation, because I think it costs people some time to find this…