有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将从Web服务接收的二进制64格式的图像转换为实际图像?

我正在用Java开发一个Web应用程序。在那个应用程序中,我用Java创建了Web服务。在该webservice中,我创建了一个webmethod,它以base64格式返回图像列表。该方法的返回类型为Vector。在webservice tester中,我可以看到SOAP响应为xsi:type="xs:base64Binary"。然后我在应用程序中调用了这个webmethod。我使用了以下代码:

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);
}

在这段代码中,我收到了错误:

java.lang.ClassCastException: [B cannot be cast to java.lang.String" on line String img = (String)itr.next();

我的密码有错误吗?或者有没有其他方法可以将图像转换为实际格式?你能提供我解决上述问题的代码或链接吗


共 (1) 个答案

  1. # 1 楼答案

    类型指示符[B告诉您从迭代器接收到的是字节数组,而不是字符串。这些字节是Base64字符串的有线格式(因此需要使用适当的编码创建一个新字符串),还是已经解码的Base64 blob,我无法确定