{"record":{"id":"2c016427f7bdf8b7","repo":"chinabugotech/hutool","slug":"image-type-is-not-supported","errorCode":null,"errorMessage":"Image type is not supported!","messagePattern":"Image type is not supported!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java","lineNumber":1995,"sourceCode":"\t}\n\n\t/**\n\t * 从流中读取图片\n\t *\n\t * @param imageStream 图片文件\n\t * @return 图片\n\t * @since 3.2.2\n\t */\n\tpublic static BufferedImage read(InputStream imageStream) {\n\t\tBufferedImage result;\n\t\ttry {\n\t\t\tresult = ImageIO.read(imageStream);\n\t\t} catch (IOException e) {\n\t\t\tthrow new IORuntimeException(e);\n\t\t}\n\n\t\tif (null == result) {\n\t\t\tthrow new IllegalArgumentException(\"Image type is not supported!\");\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * 从图片流中读取图片\n\t *\n\t * @param imageStream 图片文件\n\t * @return 图片\n\t * @since 3.2.2\n\t */\n\tpublic static BufferedImage read(ImageInputStream imageStream) {\n\t\tBufferedImage result;\n\t\ttry {\n\t\t\tresult = ImageIO.read(imageStream);\n\t\t} catch (IOException e) {\n\t\t\tthrow new IORuntimeException(e);","sourceCodeStart":1977,"sourceCodeEnd":2013,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java#L1977-L2013","documentation":"ImgUtil.read(InputStream) wraps ImageIO.read, which returns null when no reader can decode the stream. Hutool raises IllegalArgumentException (message: 'Image type is not supported!') because the resulting BufferedImage is null.","triggerScenarios":"Passing an InputStream whose bytes are not a decodable image, or a stream that has already been consumed/positioned past its header, or an unsupported format with no registered reader.","commonSituations":"Reading from a network/multipart stream whose content type is not a baseline image; reusing a stream after it was read once; compressed/encrypted bytes mistakenly fed as an image.","solutions":["Buffer the stream into a byte[] and inspect magic bytes / content type before reading.","Ensure the stream is fresh (mark/reset or re-open) and the format is supported.","Add ImageIO reader plugins for advanced formats, or convert upstream to PNG/JPG."],"exampleFix":"// before\nBufferedImage img = ImgUtil.read(request.getInputStream()); // unknown bytes\n// after\nbyte[] bytes = IoUtil.readBytes(request.getInputStream());\nif (!isSupportedImageMagic(bytes)) throw new IllegalArgumentException(\"not an image\");\nBufferedImage img = ImgUtil.read(IoUtil.toStream(bytes));","handlingStrategy":"validation","validationCode":"void requireReadableImage(byte[] b){ String[] ok={\"jpg\",\"png\",\"gif\",\"bmp\"}; if(!ArrayUtil.contains(ok, FileTypeUtil.getType(new ByteArrayInputStream(b)))) throw new IllegalArgumentException(\"not a decodable image\"); }","typeGuard":"boolean isDecodableImageStream(InputStream in) throws IOException { byte[] h = new byte[12]; in.mark(h.length); int n=IoUtil.read(in,h,0,h.length); in.reset(); return n>=3 && (isJpeg(h)||isPng(h)||isGif(h)||isBmp(h)); }","tryCatchPattern":"try { img = ImgUtil.read(in); }\ncatch (IllegalArgumentException e){ if(e.getMessage().contains(\"not supported\")) { /* re-open stream / convert */ } else throw e; }","preventionTips":["Buffer the stream and check magic bytes before reading.","Never reuse an already-consumed stream.","Confirm content-type from the source before decoding."],"tags":["image","imageio","format","io","stream","argument"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}