一、base64圖片編解碼
基本流程:從網絡獲取下載一張圖片。然后base64編碼,再base64解碼,存到本地E盤根文件夾下。
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
@SuppressWarnings("restriction")
public class Base64ImageUtils {
/** * 將網絡圖片進行Base64位編碼 * * @param imageUrl * 圖片的url路徑,如http://.....xx.jpg * @return */
public static String encodeImgageToBase64(URL imageUrl) {// 將圖片文件轉化為字節數組字符串,並對其進行Base64編碼處理
ByteArrayOutputStream outputStream = null;
try {
BufferedImage bufferedImage = ImageIO.read(imageUrl);
outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", outputStream);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 對字節數組Base64編碼
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(outputStream.toByteArray());// 返回Base64編碼過的字節數組字符串
}
/** * 將本地圖片進行Base64位編碼 * * @param imageFile * 圖片的url路徑,如F:/.....xx.jpg * @return */
public static String encodeImgageToBase64(File imageFile) {// 將圖片文件轉化為字節數組字符串,並對其進行Base64編碼處理
ByteArrayOutputStream outputStream = null;
try {
BufferedImage bufferedImage = ImageIO.read(imageFile);
outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", outputStream);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 對字節數組Base64編碼
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(outputStream.toByteArray());// 返回Base64編碼過的字節數組字符串
}
/** * 將Base64位編碼的圖片進行解碼,並保存到指定文件夾 * * @param base64 * base64編碼的圖片信息 * @return */
public static void decodeBase64ToImage(String base64, String path,
String imgName) {
BASE64Decoder decoder = new BASE64Decoder();
try {
FileOutputStream write = new FileOutputStream(new File(path
+ imgName));
byte[] decoderBytes = decoder.decodeBuffer(base64);
write.write(decoderBytes);
write.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String [] args){
URL url = null;
try {
url = new URL("http://www.faceplusplus.com.cn/wp-content/themes/faceplusplus/assets/img/demo/9.jpg");
} catch (MalformedURLException e) {
e.printStackTrace();
}
String encoderStr = Base64ImageUtils.encodeImgageToBase64(url);
System.out.println(encoderStr);
Base64ImageUtils.decodeBase64ToImage(encoderStr, "E:/", "football.jpg");
}
}
控制台輸出的base64編碼后的結果:
查看E盤根文件夾:
二、Redis對象object存儲
Redis存儲對象數據的時候。要進行對象的序列化與反序列化操作。
package RedisObject;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
public Person() {
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package RedisObject;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializeUtil {
/** * 序列化 * * @param object * @return */
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
}
return null;
}
/** * 反序列化 * * @param bytes * @return */
public static Object unserialize(byte[] bytes) {
ByteArrayInputStream bais = null;
try {
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
}
return null;
}
}
package RedisObject;
import redis.clients.jedis.Jedis;
public class PersonRedisTest {
private static Jedis jedis = null;
/** * 初始化Jedis對象 * * @throws Exception */
public PersonRedisTest() {
jedis = new Jedis("127.0.0.1", 6379);
}
/** * 序列化寫對象, 將Person對象寫入Redis中 * */
public void setObject() {
jedis.set("person:100".getBytes(),
SerializeUtil.serialize(new Person(100, "zhangsan")));
jedis.set("person:101".getBytes(),
SerializeUtil.serialize(new Person(101, "bruce")));
}
/** * 反序列化取對象, 用Jedis獲取對象 * */
public void getObject() {
byte[] data100 = jedis.get(("person:100").getBytes());
Person person100 = (Person) SerializeUtil.unserialize(data100);
System.out.println(String.format("person:100->id=%s,name=%s",
person100.getId(), person100.getName()));
byte[] data101 = jedis.get(("person:101").getBytes());
Person person101 = (Person) SerializeUtil.unserialize(data101);
System.out.println(String.format("person:101->id=%s,name=%s",
person101.getId(), person101.getName()));
}
public static void main(String[] args) {
PersonRedisTest rt = new PersonRedisTest();
rt.setObject();
rt.getObject();
}
}
執行main函數結果:
查看Redis存儲的數據:
三、Redis存儲圖片
在《Redis入門指南》一書的P22(第22頁)中,有這么一段話:
“字符串類型是Redis中最主要的數據類型,它能存儲不論什么形式的字符串,包含二進制數據。你能夠用其存儲用戶的郵箱、JSON化的對象甚至是一張圖片。”
Redis 是一個數據結構類型的server。不是單純的 key-value 存儲。 Redis 里面的鍵是二進制安全的(二進制安全是指數據在傳輸過程中保證數據的安全性,包含加密等),因此鍵的內容不應該包含空格或者換行符。比方 ”hello world” 和 ”hello world\n” 是錯誤的。
那么怎么在Redis中存儲圖片呢?說白了。圖片就是一組二進制數據,直接存儲二進制數據肯定是不行的,要實現Redis存儲二進制數據,那么就得事先實行轉化。
在一、二中已經寫了圖片base64編解碼和redis存儲對象的過程。那么我們就能夠這樣子在Redis中來存儲圖片:
- 圖片轉化成String字符串
(1)我們能夠在Redis存儲圖片的base64編碼或者解碼。以KV格式,K為普通字符串,V為圖片的base64編碼。get(K)后再base64解碼就能夠了;
(2)我們也能夠在Redis中存儲圖片的網絡url或者本地的path路徑,詳細實現能夠使圖片本身存儲到磁盤中,然后在Redis中以圖片的網絡url或者本地的path路徑為value(V)值存儲。 圖片轉化成object對象
直接在Redis中存儲圖片對象。使用Java的序列化/反序列化機制進行處理實現。