这一篇是为了记录如何从数据库获取图片流的(非保存路径)
1.作为直接将图片文件存入数据库,往往会以String类型读取,因为其图片直接存入数据库保存的类型也会是text之类的类型保存。
而往往其前面会加前缀名例如:
data:application/octet-stream;base64,
所以Base64转File文件的时候需要注意去掉前缀名
//从数据库获取图片数据文本
String base64string = xxxx.getPic().trim();
//获取到前缀的定位
int index = base64string.indexOf("base64,")+7;
String newImage = base64string.substring(index, base64string.length());//去除前缀
//将去除前缀的图片文本转化为byte流
BASE64Decoder decoder = new BASE64Decoder();
try{
// Base64解码
byte[] b = decoder.decodeBuffer(newImage);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
//将byte转换为文件
// 生成图片路径和文件名
String pathString ="E://"+xxxx.getName()+".jpg";
File file = new File(pathString);
FileOutputStream out = new FileOutputStream(pathString);
out.write(b);
out.flush();
out.close();
System.out.println("生成图片结束");
} catch (Exception e) {
}
这样就能在E盘的对应路径上生成图片文件,同时也因为这个路径可以取得这个File文件了