展开全部
看你用的是么数据库,一般是读取后转e68a84e8a2ad3231313335323631343130323136353331333431346430成二进制blob格式存入数据库的BLOB类型的字段中,另一种方法是把图片转成字符串存入数据库。
如果我没记错的话 png格式的图片(要不就是gif)都不需要转回图片,直接指定到标签的src属性上就能显示。
附上图片字符串互转的java代码:/**
* base64编码字符串转换为图片
* @param imgStr base64编码字符串
* @param path 图片路径
* @return
*/
public static boolean base64StrToImage(String imgStr, String path) {
if (imgStr == null)
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(imgStr);
// 处理数据
for (int i = 0; i
if (b[i]
b[i] += 256;
}
}
//文件夹不存在则自动创建
File tempFile = new File(path);
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(tempFile);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}/**
* 图片转base64字符串
* @param imgFile 图片路径
* @return
*/
public static String imageToBase64Str(String imgFile) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}public static void main(String[] args) {
String base64Str = imageToBase64Str("D:/pic/001.jpg");
System.out.println(base64Str);
boolean b = base64StrToImage(base64Str, "D:/pic/temp/002.jpg");
System.out.println(b);
}