无法使用 java 从文本文件解码 base64 图像

Can't decode base64 image from a text file using java

我目前正在尝试使用此方法从 Base64 解码图像,但它从未获得 getProperty() 方法

Image icon = null;
Properties properties = ReadProperties.readFromConfig("img/icon.properties");
System.setProperty("jagacy.properties.dir", "src/test/resources/img");
byte[] decoded = Base64.decode(properties.getProperty("icon"));
icon = Image.getInstance(decoded);

icon.properties 个文件包含以下内容:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABmJLR0QA/wD/AP+gvaeTAAAAcUlEQVRIiWNgGAVkgqMMDAz/ScSHsRnEiMOC/2Q6DMM8FlI14AA4HcREpAFkg1ELCAJCkUxuaoIDXD44SIZZWPMBqQCWqYgGdI9kQkUEDBBdZKBb8I8KjiYqCEkNa5zqh35GG7CcfISBtDggVf0oQAAAu0ctGvdYYksAAAAASUVORK5CYII=

here's the picture that it's supposed to be decoded

到目前为止,我只尝试解码纯文本和 URI 中的 Base64,但结果都是一样的

经过更多测试,答案最终是从文本文件中删除 image/png;base64, 并修改代码,因此工作代码如下所示:

Image icon = null;
Properties properties = ReadProperties.readFromConfig("img/icon.properties");
System.setProperty("jagacy.properties.dir", "src/test/resources/img");
byte[] decoded = Base64.decode(properties.getProperty("data")); //<-- changed from "icon" 
icon = Image.getInstance(decoded);

并且 icon.properties 文件包含以下内容:

data:iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABmJLR0QA/wD/AP+gvaeTAAAAcUlEQVRIiWNgGAVkgqMMDAz/ScSHsRnEiMOC/2Q6DMM8FlI14AA4HcREpAFkg1ELCAJCkUxuaoIDXD44SIZZWPMBqQCWqYgGdI9kQkUEDBBdZKBb8I8KjiYqCEkNa5zqh35GG7CcfISBtDggVf0oQAAAu0ctGvdYYksAAAAASUVORK5CYII=