目录
自己在项目里写的BLOB转base64 与 base64 转BLOB
//BLOB转base64
BLOB转base64后末尾会新添加两个字符与原来的base64字符串不一样,但是不影响转换为图片
//BLOB转base64
public String BLOBtobase64(ComShanDong ComShanDongall) {
byte[] blobImage = ComShanDongall.getBimage();
// String result = "";
String result = "";
if (null != blobImage) {
try {
// InputStream msgContent = blobImage.getBinaryStream();
//结尾多了两个字母
// result = Base64.getEncoder().encodeToString(blobImage);
result = Base64.getEncoder().encodeToString(blobImage);
//结尾多了两个字母
// result = new String(blobImage,(int) blobImage.length, Integer.parseInt("UTF-8"));
// result = new String(blobImage,(int) blobImage.length, Integer.parseInt("UTF-8"));
// .encodeToString(blobImage).;
// ByteArrayOutputStream output = new ByteArrayOutputStream();
// byte[] buffer = new byte[100];
// int n = 0;
//
// InputStream msgContent = blobImage.getBinaryStream();
// while (-1 != (n = msgContent.read(buffer))) {
// output.write(buffer, 0, n);
// }
// result = new BASE64Encoder().encode(output.toByteArray());
// output.close();
// comsd.setCimage(result);
} catch (Exception e) {
e.printStackTrace();
// return "erro";
// resp.setCode("1");
// resp.setMessage("失败");
}
}
return result;
}
//base64转BLOB
//base64转BLOB
public byte[] Base64toBLOB(ComShanDong ComShanDongall) {
String clobImage = ComShanDongall.getImage();
byte[] b = null;
if (null != clobImage) {
BASE64Decoder decode = new BASE64Decoder();
try {
b = decode.decodeBuffer(clobImage);
// Configuration configure = new Configuration().configure();
// SessionFactory sessionFactory = configure.buildSessionFactory();
// Session session = sessionFactory.openSession();
// Blob blob = session.getLobHelper().createBlob(bytes);
// String string = cvtBlobToBs64String(blob);
// System.out.println(string);
// return blob;
} catch (IOException e) {
e.printStackTrace();
}
}
return b;
}
咕泡学院的关于base64的代码:
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.omg.IOP.Encoding;
import sun.misc.BASE64Decoder;
import sun.security.util.Length;
import javax.sound.sampled.AudioFormat;
import java.beans.Encoder;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;
public class TEST {
public static byte[] getStringImage(String base64String) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
//将将base64格式的字符串转换为byte[] ,byte[]是字节类型的数据
return base64String != null ? decoder.decodeBuffer(base64String) : null;
}
public static void main(String[] args) throws IOException {
//将正常的字符串转换为base64格式的数据类型,编码为utf-8
String base64encodedString = Base64.getEncoder().encodeToString("升职加薪".getBytes("utf-8")); //GB2312
System.out.println("Base64 编码字符串 (基本) :" + base64encodedString);
BASE64Decoder decoder = new BASE64Decoder();
//将base64格式的数据:“yf3WsLzT0L0=”通过decoder解码成byte[]字节数据,再将byte[]数据通过new String()方法转换成GB2312编码的正常的汉字字符串,输出的"c"的值就是:升职加薪
String c = new String(decoder.decodeBuffer("yf3WsLzT0L0="),"GB2312");
System.out.println("C"+ c);
// base64encodedString = base64encodedString.substring(0, base64encodedString.length()-1);
// A[B@1b6d3586
//将base64格式的字符串 转换为byte[]
byte [] b = getStringImage(base64encodedString);
System.out.println("A"+ Arrays.toString(b));
//将byte[]类型的数据转换成base64位的编码
String result = Base64.getEncoder().encodeToString(b);
System.out.println("aaa:"+result);
}
}
自己在项目里写的BLOB转base64 与 base64 转BLOB
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.omg.IOP.Encoding;
import sun.misc.BASE64Decoder;
import sun.security.util.Length;
import javax.sound.sampled.AudioFormat;
import java.beans.Encoder;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;
public class TEST {
public static byte[] getStringImage(String base64String) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
return base64String != null ? decoder.decodeBuffer(base64String) : null;
}
public static void main(String[] args) throws IOException {
String base64encodedString = Base64.getEncoder().encodeToString("升职加薪".getBytes("utf-8")); //GB2312
System.out.println("Base64 编码字符串 (基本) :" + base64encodedString);
BASE64Decoder decoder = new BASE64Decoder();
String c = new String(decoder.decodeBuffer("yf3WsLzT0L0="),"GB2312");
System.out.println("C"+ c);
// base64encodedString = base64encodedString.substring(0, base64encodedString.length()-1);
// A[B@1b6d3586
byte [] b = getStringImage(base64encodedString);
System.out.println("A"+ Arrays.toString(b));
String result = Base64.getEncoder().encodeToString(b);
System.out.println("aaa:"+result);
}
}