jedis takes redis picture and converts it into Base64
jedis memory object
/** * Serialization * * @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;
}
/** * Deserialization * * @param bytes * @return */
public static Object unserialize(byte[] bytes) {
ByteArrayInputStream bais = null;
try {
ObjectInputStream inputStream;
inputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
Object obj = inputStream.readObject();
} catch (Exception e) {
}
return null;
}
jedis gets the picture inside redis and turns it into Base64
/**
* Convert bytes to objects
*/
public Object byte2Object(byte[] bytes) {
if (bytes == null || bytes.length == 0)
return null;
try {
ObjectInputStream inputStream;
inputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
Object obj = inputStream.readObject();
return obj;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
Get Base64 characters
String imgBase64Str = Base64Utill.getImgBase64Str(
(byte[])(byte2Object(jedis.get(key.getBytes()))) );
Transfer to Base64 tool class
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.stream.FileImageInputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64Utill {
/**
*
* @Description Convert bytes to Base64 Documents
* @param data Bytes to convert
* @param fileType File type
* @return Base64 String
*/
public static String getImgBase64Str(byte[] data, String fileType) throws IOException {
String fileContentBase64 = null;
String base64Str = "data:" + fileType + ";base64,";
String content = null;
// Byte array Base64 Code
if (data == null || data.length == 0) {
return null;
}
BASE64Encoder encoder = new BASE64Encoder();
content = encoder.encode(data);
if (content == null || "".equals(content)) {
return null;
}
fileContentBase64 = base64Str + content;
return fileContentBase64;
}
/**
*
* @Description Convert bytes to Base64
* @param data Bytes to convert
* @return Base64 String
*/
public static String getImgBase64Str(byte[] data) throws IOException {
String fileContentBase64 = null;
String content = null;
// Byte array Base64 Code
if (data == null || data.length == 0) {
return null;
}
BASE64Encoder encoder = new BASE64Encoder();
content = encoder.encode(data);
if (content == null || "".equals(content)) {
return null;
}
fileContentBase64=content;
return fileContentBase64;
}
/**
*
* @Description Convert bytes to Base64 Documents
* @param file File to convert
* @param fileType File type
* @return Base64 String
*/
public static String getImgBase64Str(File file) {
String fileContentBase64 = null;
String content = null;
// Converts a picture file into a byte array string, and performs the Base64 Coding processing
InputStream in = null;
// Read the picture byte array
try {
in = new FileInputStream(file);
byte[] data = new byte[in.available()];
in.read(data);
in.close();
// Byte array Base64 Code
if (data == null || data.length == 0) {
return null;
}
BASE64Encoder encoder = new BASE64Encoder();
content = encoder.encode(data);
if (content == null || "".equals(content)) {
return null;
}
return encoder.encode(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return fileContentBase64;
}
/**
*
* @Description Convert bytes to Base64 Documents
* @param file File to convert
* @param fileType File type
* @return Base64 String
*/
public static String getImgBase64Str(File file, String fileType) throws IOException {
String fileContentBase64 = null;
String base64Str = "data:" + fileType + ";base64,";
String content = null;
// Converts a picture file into a byte array string, and performs the Base64 Coding processing
InputStream in = null;
// Read the picture byte array
try {
in = new FileInputStream(file);
byte[] data = new byte[in.available()];
in.read(data);
in.close();
// Byte array Base64 Code
if (data == null || data.length == 0) {
return null;
}
BASE64Encoder encoder = new BASE64Encoder();
content = encoder.encode(data);
if (content == null || "".equals(content)) {
return null;
}
fileContentBase64 = base64Str + content;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
}
return fileContentBase64;
}
/**
*
* @Description base64 Transbyte
* @param base64Str String
* @return byte[]
*/
public static byte[] base64String2Byte(String base64Str){
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = null;
try {
b = decoder.decodeBuffer(base64Str);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
/**
* Convert a picture to Base64 Code
* @param imgFile Picture to be processed
* @return
*/
public static String getImgBase64Str(String imgFile) {
// Converts a picture file into a byte array string, and performs the Base64 Coding processing
InputStream in = null;
byte[] data = null;
// Read the picture byte array
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
/**
* Convert a picture to a byte array
* @param imgFile Picture to be processed
* @return
*/
public byte[] image2byte(String path){
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
}
catch (FileNotFoundException ex1) {
ex1.printStackTrace();
}
catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
}
/**
* On a byte array string Base64 Decode and generate pictures
* @param imgStr Picture data
* @param imgFilePath Save the full path address of the picture
* @return
*/
public static boolean generateImage(String imgStr, String imgFilePath) {
//
if (imgStr == null) // Image data is empty
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64 Decoding
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// Adjust exception data
b[i] += 256;
}
}
// Generate jpg Picture
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}
b[i] += 256;
}
}
// Generate jpg Picture
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}