/**
* 从数据库blob类型得到图片
*/
public static String getImageForBlob(byte[] bytes,String name) {
// 读取文件的前几个字节来判断图片格式
byte[] b = new byte[4];
for (int i = 0; i < b.length; i++) {
b[i] = bytes[i];
}
String type = HexUtil.encodeHexStr(b).toUpperCase();;
if (type.contains("FFD8FF")) {
type = "JPG";
} else if (type.contains("89504E47")) {
type = "png";
} else if (type.contains("47494638")) {
type = "GIF";
} else if (type.contains("424D")) {
type = "BMP";
}else{
type = "jpeg";
}
// 设置文件地址
String destDir = "d:/pic";
String path = StrUtil.format("{}/{}."+type, destDir,name );
// 将byte[]转换为输入流写入文件
InputStream sbs = new ByteArrayInputStream(bytes);
FileUtil.writeFromStream(sbs, path);
return urlPath;
}
java 从blob类型得到图片文件
最新推荐文章于 2024-05-01 19:17:00 发布