How to cast an image received from a webservice in Binary64 format to actual image?
I am developing a Web application in Java. In that appli开发者_如何学运维cation, 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?
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.
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
精彩评论