1.将本地的图片转成byte数组,base64序列化,转成byte数组,保存为jpeg
package p01;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;
import com.fasterxml.jackson.databind.ObjectMapper;
/*
* 1. 图片转为byte数组,byte数组转为,base64 String
* 2. String 转回 byte数组,在本地保存为图片
* 3. 将java 对象转化为json 数据
*/
public class ImgToString {
public static void main(String []args) throws Exception {
// 文件名的list
List list = new ArrayList();
// 先从本地读取图片,转成String
String strF1 = "f:\\115图片20170210174749.png";
list.add(strF1);
File f1 = new File("");
String uuid = (UUID.randomUUID().toString()).replaceAll("-", "");
String strF2 = "f:\\img\\desk\\" + uuid + ".JPEG";
list.add(strF2);
File f2 = new File(strF2);
File dir = new File("f:\\img\\desk\\");
// 创建文件夹
dir.mkdirs();
Util util = new Util();
// 将list转为json
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(list);
System.out.println(json);
if (f1.exists()) {
byte []data = null;
FileImageInputStream input = new FileImageInputStream(f1);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte []buf = new byte[1024];
int numBytesRead = 0;
// 返回读取多少个byte
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
String strImg = util.encodeBase64(data);
// System.out.println(strImg);
System.out.println("正在转码");
input.close();
output.close();
// 返序列化
// 将字符串转化为图片
byte []dataImg = util.decodeBase64(strImg);
FileImageOutputStream fios = new FileImageOutputStream(f2);
fios.write(dataImg, 0, dataImg.length);
fios.close();
System.out.println("ok");
} else {
System.out.println("null");
}
System.out.println("end");
}
}
2.
接受base64 的string,保存在本地,返回json格式的图片路径
package com.yuejianmian.util;
import java.io.File;
import java.lang.reflect.Method;
import java.util.UUID;
import javax.imageio.stream.FileImageOutputStream;
import com.fasterxml.jackson.databind.ObjectMapper;
public class StringUtil {
public String saveImgLocal(String basePath, String imgStr) throws Exception {
// 创建文件夹
File dir = new File(basePath);
// 判断是否存在,不存在创建
if (!dir.exists()) {
dir.mkdirs();
}
// uuid
String uuid = (UUID.randomUUID().toString()).replaceAll("-", "");
String imgPath = basePath + uuid + ".JPEG";
// 创建图片流
FileImageOutputStream fios = null;
// 将String 转成byte数组
byte []dataImg = decodeBase64(imgStr);
// 写入本地
File img = new File(imgPath);
fios = new FileImageOutputStream(img);
fios.write(dataImg, 0, dataImg.length);
fios.close();
return imgPath;
}
// jackson插件,将一个对象转成一个json字符串
public String objToJSONString(Object obj) throws Exception {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(obj);
}
/***
* encode by Base64
*/
private byte[] decodeBase64(String input) throws Exception{
Class clazz=Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
Method mainMethod= clazz.getMethod("decode", String.class);
mainMethod.setAccessible(true);
Object retObj=mainMethod.invoke(null, input);
return (byte[])retObj;
}
}
/***
* decode by Base64
* 将字符串转化为图片,序列化
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static byte[] decodeBase64(String input) throws Exception{
Class clazz=Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
Method mainMethod= clazz.getMethod("decode", String.class);
mainMethod.setAccessible(true);
Object retObj=mainMethod.invoke(null, input);
return (byte[])retObj;
}