You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用FileInputStream读取图片时为何出现-1字节?

问题:FileInputStream读取图片出现-1字节,解密后图片异常但MD5一致

我在用FileInputStream读取图片时,发现流中存在一个-1字节。通过自定义的BNCChuckChainOutputStream和BNCChuckChainInputStream完成加密解密操作后,生成的图片无法正常显示,但我通过调试工具验证了原始文件和解密后文件的MD5值完全一致,因此认为这两个自定义流的逻辑是正常的。

相关代码如下:

public class Main {
    public static void main(String[] args) throws Throwable {
        long stt = System.currentTimeMillis();
        String file = "test/dlk.png", to = "test/test.png", output = file + ".bncchain.zip";
        byte[] password = "DLK IS SO HANDSOME".getBytes();
        String chainName = "dlk";

        try (BNCChuckChainOutputStream out = new BNCChuckChainOutputStream(new FileOutputStream(output), chainName, password, 16384); FileInputStream in = new FileInputStream(file)) {
            in.transferTo(out);
        }
        long edt = System.currentTimeMillis();
        System.out.println("Encrypt TimeUsed:" + (edt - stt) + " ms");

        stt = System.currentTimeMillis();
        try (BNCChuckChainInputStream in = new BNCChuckChainInputStream(new FileInputStream(output), chainName, password); FileOutputStream fos = new FileOutputStream(to)) {
            in.transferTo(fos);
        }
        edt = System.currentTimeMillis();
        System.out.println("Decrypt TimeUsed:" + (edt - stt) + " ms");

    }
}

可供对比的文件:原始图片文件、解密后的错误图片文件;附写入操作时的调试截图。

内容的提问来源于stack exchange,提问作者NiceCat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:39:18