简体   繁体   English

如何将CQ用户配置文件图像转换为base64编码的字符串

[英]How to convert cq user profie image to base64 encoded string

I uploded profile pic through granite operations. 我通过花岗岩作业提升了个人资料照片。 If we upload profile pic here, it is adding the jcr.data property as binary data under /image/jcr:content ex: /home/users/s/sampleuser/profile/photos/primary/image/jcr:content 如果我们在此处上传个人资料照片,则它将jcr.data属性作为二进制数据添加到/ image / jcr:content下:/ home / users / s / sampleuser / profile / photos / primary / image / jcr:content 在此处输入图片说明

I am trying to convert this binary data using org.apache.commons.codec.binary.Base64 but it is not returning the correct encoded string. 我正在尝试使用org.apache.commons.codec.binary.Base64转换此二进制数据,但是它没有返回正确的编码字符串。

Code which I am trying: 我正在尝试的代码:

Node photoNode = node.getNode("photos/primary/image/jcr:content");
javax.jcr.Binary binaryData = photoNode.getProperty("jcr:data").getBinary();
InputStream input = binaryData.getStream();
int BUFF_SIZE=1024;
byte[] line=new byte[BUFF_SIZE];
ByteArrayOutputStream output=new ByteArrayOutputStream();
while (input.read(line) != -1) {
output.write(line);
}
Base64 base64=new Base64();
byte[] encoded=base64.encode(output.toByteArray());
String enodedString = Base64.encodeBase64String(encoded);
LOG.info("is Base64::"+Base64.isBase64(enodedString));
LOG.info("enodedString"+enodedString);
photo = enodedString;

Here is what I did: 这是我所做的:

InputStream is = null;
try {
    is = binaryData.getStream();
    byte[] bytes = IOUtils.toByteArray(is);
    String base64 = Base64.encodeBase64String(bytes);
}
catch (IOException e) {
}
finally {
    IOUtils.closeQuietly(is)
}

IOUtils from apache-io is your friend here. apache-io的IOUtils是您的朋友。 Not sure what is wrong with your code as it looks ok, though a bit cumbersome with the buffer. 虽然看起来有点麻烦,但不确定缓冲区的代码出了什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM