Java 使用Base64将图片保存到数据库和显示图片
Java Base64保存图片
================================
©Copyright 蕃薯耀 2019年5月28日
http://fanshuyao.iteye.com/
一、将图片以Base64保存到数据库blob字段中
import org.apache.commons.io.FileUtils;
import sun.misc.BASE64Encoder;
扫描二维码关注公众号,回复:
6293026 查看本文章
/** * 以Base64保存图片到数据库 * @param request * @param file * @throws Exception */ public void saveImage(HttpServletRequest request, File file) throws Exception{ byte[] fileByteArray = FileUtils.readFileToByteArray(file); BASE64Encoder encoder = new BASE64Encoder(); String imageString = encoder.encode(fileByteArray); String sql = "update image_table set image = ? where id = ?"; List<Object> params = new ArrayList<Object>(); params.add(imageString); params.add(1); getDao().update(sql, params); }
================================
©Copyright 蕃薯耀 2019年5月28日
http://fanshuyao.iteye.com/