图片由于文件比较大,常用的图片如多采用读取数据库或者文件的方式会加大系统的负载。采用缓存保存的方法可以增加效率。
http://stackoverflow.com/questions/13215024/weird-redis-key-with-spring-data-jedis 这里已经提供了图片的读取方法和类型转换方法,修改一下即可。
本文采用的是,将工程下的文件读入到redis缓存缓存数据库中。
读取文件和编码图片为字符串
public static String encodeToString(BufferedImage image, String type) { //将<span style="font-family: Arial, Helvetica, sans-serif;">BufferedImage 转化为string,type是文件类型:jpg,png等</span>
String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
imageString = encoder.encode(imageBytes);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}