chinabugotech/hutool · error · IllegalArgumentException
Image type of [{}] is not supported!
Error message
Image type of [{}] is not supported! What it means
ImgUtil.read(URL) wraps ImageIO.read(URL); when no reader decodes the URL content it returns null, and Hutool raises IllegalArgumentException naming the URL ('Image type of [{}] is not supported!').
Source
Thrown at hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java:2039
}
/**
* 从URL中读取图片
*
* @param imageUrl 图片文件
* @return 图片
* @since 3.2.2
*/
public static BufferedImage read(URL imageUrl) {
BufferedImage result;
try {
result = ImageIO.read(imageUrl);
} catch (IOException e) {
throw new IORuntimeException(e);
}
if (null == result) {
throw new IllegalArgumentException("Image type of [" + imageUrl + "] is not supported!");
}
return result;
}
/**
* 获取{@link ImageOutputStream}
*
* @param out {@link OutputStream}
* @return {@link ImageOutputStream}
* @throws IORuntimeException IO异常
* @since 3.1.2
*/
public static ImageOutputStream getImageOutputStream(OutputStream out) throws IORuntimeException {
ImageOutputStream result;
try {
result = ImageIO.createImageOutputStream(out);
} catch (IOException e) {View on GitHub (pinned to 8870454b2a)
Solutions
- Download the bytes, verify content-type/magic bytes, then read via ImgUtil.read(InputStream) with a confirmed format.
- Install ImageIO plugins for the content type you expect (WEBP/AVIF/HEIC).
- Check HTTP status and content-type before attempting to decode.
Example fix
// before
BufferedImage img = ImgUtil.read(new URL("https://example.com/icon.svg"));
// after - fetch and verify
byte[] b = HttpUtil.downloadBytes("https://example.com/icon.png");
BufferedImage img = ImgUtil.read(IoUtil.toStream(b)); Defensive patterns
Strategy: validation
Validate before calling
void requireImageUrl(URL u) throws IOException { HttpURLConnection c=(HttpURLConnection)u.openConnection(); c.setRequestMethod("HEAD"); String ct=c.getContentType(); if(ct==null || !ct.startsWith("image/")) throw new IllegalArgumentException("URL is not an image: "+ct); } Type guard
boolean isImageUrl(URL u) throws IOException { HttpURLConnection c=(HttpURLConnection)u.openConnection(); c.setRequestMethod("HEAD"); String ct=c.getContentType(); return ct!=null && ct.startsWith("image/"); } Try / catch
try { img = ImgUtil.read(url); }
catch (IllegalArgumentException e){ if(e.getMessage().contains("not supported")) { byte[] b=HttpUtil.downloadBytes(url.toString()); img=ImgUtil.read(IoUtil.toStream(b)); } else throw e; } Prevention
- Verify HTTP status and content-type before decoding URLs.
- Download to bytes first so you can inspect magic bytes.
- Install ImageIO plugins for modern formats you expect.
When it happens
Trigger: Reading an image from a URL whose content is not a baseline-decodable image (SVG/AVIF/HEIC), returns an HTML error page, or a content-type the JVM cannot decode; network/redirect issues yielding non-image bytes.
Common situations: Fetching avatars/icons that are SVG; URLs that 404 and return text; content negotiation returning WEBP without a reader; CDN redirects not producing image bytes.
Related errors
AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14).
Data as JSON: /api/errors/1b97ee78280a769e.
Report an issue: GitHub.