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



我目前正在尝试使用此方法从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);

图标。属性文件包含以下内容:

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

这是应该被解码的图片

到目前为止,我只尝试在纯文本和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);

和图标。属性文件包含以下内容:

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

最新更新