views:

137

answers:

2

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?

+2  A: 

The type indicator [B tells you that you have received a byte array, rather than a string from your iterator. Whether those bytes are the wire format for the Base64 string (so needing to create a new string with the appropriate encoding), or the already decoded Base64 blob, I cannot tell.

Steve Gilham
+1  A: 

Indeed, i agree with Steve. Try debugging at that line and see what concrete type iter.next() returns, or do something like this:

Object next = iter.next();
System.out.println(next.getClass())

That should tell you what the concrete class is, then you just have to adjust your code to use that class instead of String.

HTH

simonlord