{"record":{"id":"2fda75232eb20afd","repo":"jeecgboot/JeecgBoot","slug":"base64","errorCode":null,"errorMessage":"生成验证码base64失败","messagePattern":"生成验证码base64失败","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/util/RandImageUtil.java","lineNumber":102,"sourceCode":"     */\n    public static String generate(String verifyCode) throws IOException {\n        if (verifyCode == null || verifyCode.trim().isEmpty()) {\n            throw new IllegalArgumentException(\"验证码不能为空\");\n        }\n        \n        try {\n            BufferedImage image = createVerifyCodeImage(verifyCode);\n            \n            try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {\n                ImageIO.write(image, IMG_FORMAT, byteStream);\n                byte[] bytes = byteStream.toByteArray();\n                String base64 = Base64.getEncoder().encodeToString(bytes).trim();\n                // 清理换行符\n                base64 = base64.replaceAll(\"[\\r\\n]\", \"\");\n                return BASE64_PREFIX + base64;\n            }\n        } catch (Exception e) {\n            throw new IOException(\"生成验证码base64失败\", e);\n        }\n    }\n\n    /**\n     * 创建验证码图像\n     * \n     * @param verifyCode 验证码字符串\n     * @return 验证码图像\n     */\n    private static BufferedImage createVerifyCodeImage(String verifyCode) {\n        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);\n        Graphics2D graphics = null;\n        \n        try {\n            graphics = (Graphics2D) image.getGraphics();\n            \n            // 设置图形渲染质量\n            setupRenderingHints(graphics);","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/jeecgboot/JeecgBoot/blob/96fb33f5ec68516da0b0147da06b2eb0419e063a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/util/RandImageUtil.java#L84-L120","documentation":"Thrown by RandImageUtil.generate(String) — the base64 overload — when ImageIO.write fails to encode the BufferedImage into a ByteArrayOutputStream, or when Base64 encoding fails. Like error 267, createVerifyCodeImage has its own fallback (createErrorImage), so the root cause is usually in ImageIO.write or ByteArrayOutputStream operations. This variant returns a data URI string (data:image/jpg;base64,...) and is typically used by frontends that render the captcha from base64.","triggerScenarios":"Headless environment where JPEG codec is unavailable or misconfigured. BufferedImage creation fails silently and createErrorImage also encounters an issue. Extremely low memory causing ByteArrayOutputStream or Base64 encoder to throw OutOfMemoryError (which extends Error, not Exception, so it would propagate differently — but the catch block catches Exception). Corrupted AWT/GraphicsEnvironment on the JVM.","commonSituations":"Minimal Docker images without libfontconfig or JPEG native libraries. JDK distribution that lacks the JPEG ImageIO plugin (some compact profiles or JRE strips). Server running with very tight heap limits where 200 interference lines + image buffer exhaust memory under load.","solutions":["Install font and image libraries in the container: apt-get install -y fontconfig fonts-dejavu-core libjpeg62-turbo (Debian) or apk add fontconfig ttf-dejavu libjpeg-turbo (Alpine).","Verify the JDK has the JPEG ImageIO plugin: run ImageIO.getWriterFormatNames() in a test — it should include 'JPEG' or 'jpg'.","If using a stripped JRE, switch to a full JDK or add the javax.imageio JPEG plugin dependency (com.twelvemonkeys.imageio:imageio-jpeg).","Reduce INTERFERENCE_LINE_COUNT (currently 200) if memory is tight, or increase JVM heap (-Xmx).","Wrap the call site in try-catch and return a fallback captcha mechanism (e.g., text-based math captcha) if image generation is unavailable."],"exampleFix":"// before: unhandled IOException at call site\nString base64Captcha = RandImageUtil.generate(verifyCode);\nreturn Result.OK(base64Captcha);\n\n// after: graceful fallback on image generation failure\ntry {\n    String base64Captcha = RandImageUtil.generate(verifyCode);\n    return Result.OK(base64Captcha);\n} catch (IOException e) {\n    log.error(\"验证码图片生成失败，使用备用方案\", e);\n    return Result.OK(\"data:image/png;base64,\" + generateFallbackTextCaptcha(verifyCode));\n}","handlingStrategy":"try-catch","validationCode":"// Validate environment can produce images before relying on base64 captcha\npublic boolean isImageIOWAvailable() {\n    try {\n        BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);\n        ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        boolean canWrite = ImageIO.write(img, \"JPEG\", baos);\n        return canWrite;\n    } catch (Exception e) {\n        return false;\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    String base64 = RandImageUtil.generate(verifyCode);\n    return Result.OK(base64);\n} catch (IOException e) {\n    log.error(\"Base64 captcha generation failed\", e);\n    // Fallback: return a simple text-based challenge\n    return Result.OK(\"VERIFY:\" + verifyCode);\n}","preventionTips":["Install fontconfig and JPEG libraries in the container image (fonts-dejavu-core, libjpeg).","Verify ImageIO has a JPEG writer: ImageIO.getImageWritersByFormatName(\"JPEG\").hasNext().","If using a minimal JRE, ensure the javax.imageio JPEG plugin is present or add a third-party plugin.","Provide a non-image fallback captcha mechanism for environments where image generation is unreliable.","Monitor for this error in production — it indicates an infrastructure/font issue, not a code bug."],"tags":["captcha","base64","imageio","headless","jeecg-boot"],"backgroundTag":null,"analyzedSha":"96fb33f5ec68516da0b0147da06b2eb0419e063a","analyzedAt":"2026-08-14T00:04:16.786Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}