惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog
WordPress大学
WordPress大学
小众软件
小众软件
IT之家
IT之家
腾讯CDC
月光博客
月光博客
量子位
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - digdeep

Tomcat配置支持软连接 记一例DNS解析导致的系统卡顿问题 nginx配置透传自定义文件头 访问RustFS中的图片时,浏览器报错 (failed)net::ERR_BLOCKED_BY_ORB mysql主从复制报错:Last_Errno: 1872 Last_Error: Slave failed to initialize relay log info structure from the repository 华为云平台的操作系统因为OOM而重启的解决方法 passwd 修改密码报错“passwd: 鉴定令牌操作错误” mysql主从复制报错:Last_SQL_Errno: 1114 Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s) 解决Linux下生成PDF中文乱码的问题 mysql主从复制从库报错:Last_Errno: 1594 centos编译安装opencv mysql搭建主从复制时一直报错 Last_IO_Errno: 2026 kswapd0进程占用cpu非常高 ./configure: error: SSL modules require the OpenSSL library. maven mvn install 报错: unable to find valid certification path to requested target 错误 升级Nginx,--with-openssl踩坑记录 Name or service not known Kernel panic - not syncing: Out of memory: system-wide panic_on_oom is enabled ERROR 2059 (HY000): Plugin caching_sha2_password could not be loaded gh-ost 报错 ERROR 1236 (HY000): A slave with the same server_uuid/server_id as this slave
javax.imageio.IIOException: Invalid icc profile: duplicat...
digdeep · 2026-06-05 · via 博客园 - digdeep

图片文件上传代码如下:

BufferedInputStream in = new BufferedInputStream(file.getInputStream());

BufferedImage bi = ImageIO.read(in); 

BufferedImage bi = ImageIO.read(in); 这一行突然报错:

javax.imageio.IIOException: Invalid icc profile: duplicate sequence numbers
	at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(Native Method)
	at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(JPEGImageReader.java:620)
	at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:347)
	at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:492)
	at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:613)
	at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1070)
	at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1050)
	at javax.imageio.ImageIO.read(ImageIO.java:1448)
	at javax.imageio.ImageIO.read(ImageIO.java:1352)

图片解析异常:Invalid icc profile: duplicate sequence numbers

异常核心原因

这是JPEG 图片本身的 ICC 色彩配置文件损坏 / 不标准导致的:

图片里的ICC Profile(色彩配置文件)存在重复的序列号,Java 自带的 ImageIO 解析器无法识别这种非标准、损坏的 JPEG 文件,直接抛出异常。

简单说:图片文件是坏的 / 格式不标准,Java 读不了。

解决办法:

BufferedImage bi = ImageIO.read(in); 
换成:
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class ImageUtil {

    public static BufferedImage readImage(InputStream inputStream) throws IOException {
        // 读取流为字节数组,防止流只能读一次
        byte[] bytes = toByteArray(inputStream);
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);

        BufferedImage image;
        try {
            // 正常图片读取
            image = ImageIO.read(bais);
        } catch (Exception e) {
        	e.printStackTrace();
            // 处理ICC异常图片
            Image rawImage = Toolkit.getDefaultToolkit().createImage(bytes);
            // 强制等待图片加载完成(解决宽高-1问题)
            Image loadedImage = new ImageIcon(rawImage).getImage();

            int width = loadedImage.getWidth(null);
            int height = loadedImage.getHeight(null);

            if (width <= 0 || height <= 0) {
                throw new IOException("图片已损坏,无法解析");
            }

            // JDK8 正确写法:手动关闭 Graphics2D
            image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = image.createGraphics();
            g2d.drawImage(loadedImage, 0, 0, null);
            g2d.dispose(); // 手动释放资源
        }

        if (image == null) {
            throw new IOException("不支持的图片格式或文件已损坏");
        }
        return image;
    }

    // JDK8 流转字节数组
    private static byte[] toByteArray(InputStream is) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int len;
        while ((len = is.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        return baos.toByteArray();
    }
}

 解决问题。 

系统运行了好几年,突然有个用户反馈报错。报错的核心原因是图片格式非法。