how to convert byte array to image in java?
Posted
by nemade-vipin
on Stack Overflow
See other posts from Stack Overflow
or by nemade-vipin
Published on 2010-03-12T05:56:07Z
Indexed on
2010/03/12
5:57 UTC
Read the original article
Hit count: 311
I am developing a Web application in Java. In that application, I have created webservices in Java. In that webservice, I have created one webmethod which returns the image list in base64 format. The return type of the method is Vector. In webservice tester I can see the SOAP response as xsi:type="xs:base64Binary". Then I called this webmethod in my application. I used the following code:
SBTSWebService webService = null; List imageArray = null; List imageList = null; webService = new SBTSWebService(); imageArray = webService.getSBTSWebPort().getAddvertisementImage(); Iterator itr = imageArray.iterator(); while(itr.hasNext()) { String img = (String)itr.next(); byte[] bytearray = Base64.decode(img); BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytearray)); imageList.add(imag); } In this code I am receiving the error:
java.lang.ClassCastException: [B cannot be cast to java.lang.String" on line String img = (String)itr.next();
Is there any mistake in my code? Or is there any other way to bring the image in actual format? Can you provide me the code or link through which I can resolve the above issue?
Note:- I already droped this question and I got the suggetion to try the following code
Object next = iter.next(); System.out.println(next.getClass())
I tried this code and got the output as byte[] from webservice. but I am not able to convert this byte array to actual image. is there any other way to bring the image in actual format? Can you provide me the code or link through which I can resolve the above issue?
© Stack Overflow or respective owner