我想要做的是从Web服务的图像转印到移动客户端。为了做到这一点,我已经创建了一个返回一个字节[]变量的网络服务操作。在该方法我创建从一个图表的.PNG图像。在此之后,我从图像获取字节并为他们提供作为操作的返回值。这是服务器代码:

public byte[] getBytes() throws IOException { 

BufferedImage chartImage = chart.createBufferedImage(230, 260); 
//I get the image from a chart component. 
ByteArrayOutputStream baos = new ByteArrayOutputStream(1000); 


ImageIO.write( chartImage, "png",baos ); 


baos.flush(); 
byte[] bytesImage = baos.toByteArray(); 

baos.close(); 

return bytesImage; 
} 

Now in the mobile application all i do is assign a byte[] variable the return value of the web service operation.

byte[] imageBytes = Stub.getBytes().

也许我失去了一些东西,但是这是行不通的,因为我得到这个运行时错误:

java.rmi.MarshalException: Expected Byte, received: iVBORw0KGgoAAAANSUhEU.... (very long line).

有任何想法,为什么这happends?或者,也许你可以提出任何其他方式将数据发送到移动客户端。

有帮助吗?

解决方案

如果该服务只提供一个图像作为一个字节数组,开销诱导通过在客户端上的SOAP响应和XML / SOAP解析包裹这似乎相当不必要的。你为什么不执行图表生成servlet中,让客户从“非SOAP”服务器URL检索图像?

,而不是从一个WebService方法等你做返回bytesImage的,可以代替写字节数组servlet的响应对象:

response.setContentType("image/png");
response.setContentLength(bytesImage.length);
OutputStream os = response.getOutputStream();
os.write(bytesImage);
os.close();

在J2ME客户机,你会读取来自URL的响应,向其中该servlet结合,并从数据创建一个图像:

HttpConnection conn = (HttpConnection)Connector.open("http://<servlet-url>");
DataInputStream dis = conn.openDataInputStream();
byte[] buffer = new byte[conn.getLength()];
dis.readFully(buffer);
Image image = Image.createImage(buffer, 0, buffer.length);

希望这有助于!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top