我已使用Java代码将Base64图像插入数据库
FileInputStream mFileInputStream = new FileInputStream("C:\\basicsworkspace\\base64upload\\src\\main\\resources\\basic.png");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int bytesRead = 0;
while ((bytesRead = mFileInputStream.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byte[] ba = bos.toByteArray();
//byte[] encoded = Base64.getEncoder().encode(ba); // util
byte[] encoded = Base64.encodeBase64(ba); //apache
connection = DriverManager.getConnection(connectionString);
String insertSql = "INSERT INTO test (image) VALUES (?)" ;
prepsInsertProduct.setBytes(encoded);
System.out.println(insertSql);
prepsInsertProduct = connection.prepareStatement(
insertSql);
System.out.println(prepsInsertProduct.execute());
但是,如果尝试使用angular js显示图像,则不会显示图像。在sqlserver中,我将图像保存为varbinary(max)并以角度代码保存,
config(function ($compileProvider) {
console.log($compileProvider.imgSrcSanitizationWhitelist());
})
<img src="choice:image/png;base64,{{choice.Icon}}">
但是我得到的字节只有这样的消息,
unsafe:choice:image/png;base64,6956424F5277304B47676F414141414E535568455567…
Please let me know where I am going wrong and the image is in PNG format.
在POJO中,我对byte []进行了更改,并且可以正常工作。我的数据类型为String并修改为byte [],但未显示Base64图像,但t仅显示字节数组图像。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句