Java 将ByteArray转换为Base64
如何将这样的图像转换为Base64字符串? 我用React将图像上传到服务器,缩放图像,保存在数据库中,再次读取图像并获得此格式。 现在我必须将此格式转换为Base64格式 缩放图像的Java代码:Java 将ByteArray转换为Base64,java,Java,如何将这样的图像转换为Base64字符串? 我用React将图像上传到服务器,缩放图像,保存在数据库中,再次读取图像并获得此格式。 现在我必须将此格式转换为Base64格式 缩放图像的Java代码: Part bild = request.getPart("bild"); InputStream in = null; long fileSize = bild.getSize(); byte[] bytesBild
Part bild = request.getPart("bild");
InputStream in = null;
long fileSize = bild.getSize();
byte[] bytesBild = null;
if (fileSize > 0) {
in = bild.getInputStream();
BufferedImage imBuff = ImageIO.read(bild.getInputStream());
BufferedImage resize = resizeImage(imBuff, 200, 200);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(resize, "jpeg", os); // Passing: (RenderedImage im, String formatName, OutputStream
// output)
InputStream is = new ByteArrayInputStream(os.toByteArray());
bytesBild = IOUtils.toByteArray(is);
}
这是来自我的数据库的结果:
您可以使用本机类 保存到数据库 从数据库读取
你试过在网上搜索吗?
//...
bytesBild = IOUtils.toByteArray(is);
String imgEncodedString = Base64.getEncoder().encodeToString(bytesBild);
//imgEncodedString --> ddbb
byte[] imgDecodedBytes = Base64.getDecoder().decode(imgEncodedString);
ByteArrayInputStream bis = new ByteArrayInputStream(imgDecodedBytes);
//...