我有以下代码可以将字节数组保存为图像。
使用以下代码我可以成功地保存图像。
目前我正在使用“.png”格式保存图像,但我想从字节数组中获取图像扩展名并以该扩展名保存图像。
这是我的代码
public boolean SaveImage(String imageCode) throws Exception {
boolean status = false;
Connection dbConn = null;
CallableStatement callableStatement = null;
try {
String base64Image = imageCode.split(",")[1];
byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image);
Properties propFile = LoadProp.getProperties();
String filepath = propFile.getProperty(Constants.filepath);
File file = new File(filepath + "xyz.png");
FileOutputStream fos = new FileOutputStream(file);
try {
fos.write(imageBytes);
} finally {
fos.close();
}
} catch (Exception e) {
throw e;
} finally {
if (callableStatement != null) {
callableStatement.close();
}
if (dbConn != null) {
dbConn.close();
}
}
return status;
}
我正在使用Java和Tomcat 8。