首页 > 解决方案 > byte array to image file in java

问题描述

enter image description here

attached is an image file which i convert to byte array and writes back the byte array to a image file . it works fine . the value of the converted byte array is [B@5bc79255.

same file is attached ina n incident in HP service manager. and am using java wo connect trhough web service and download the attachment file as byte array and write to a image file . in this situation i face problem. this byte array which i retrieve from HPSM through the webservice function provided by them is [B@6e0e048a

as you can see values are also different. but surprisingly when i write [B@6e0e048a in to a image file , the size of the file is sero and nothing is there inside the file !!! where as if i convert the same file as byte array and writes back in to some image file its reproduced correctly!!

标签: javaarrays

解决方案


如果您使用 servlet 来检索字节并且图像是 base64 图像,请使用:

while ((line = request.getReader().readLine()) != null)
                stringBuffer.append(line);

String base64Image = imageData.toString().split(",")[1];
            byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image);
            ByteArrayInputStream finalStream = new ByteArrayInputStream(stringBuffer);

并将 finalStream 写入文件。


推荐阅读