【问题标题】:web service - mobile application (j2me) file transferWeb 服务 - 移动应用程序 (j2me) 文件传输
【发布时间】:2009-09-29 13:21:07
【问题描述】:

我正在尝试将图像从网络服务传输到移动客户端。为了做到这一点,我创建了一个返回 byte[] 变量的 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).

知道为什么会发生这种情况吗?或者,也许您可​​以建议任何其他方式将数据发送到移动客户端。

【问题讨论】:

    标签: file java-me transfer


    【解决方案1】:

    如果服务仅以字节数组的形式提供图像,则通过将其包装在 SOAP 响应中而产生的开销以及客户端上的 XML/SOAP 解析似乎是不必要的。为什么不在 servlet 中实现图表生成并让客户端从“非 SOAP”服务器 URL 检索图像?

    您可以将字节数组写入 servlet 的响应对象,而不是像您一样从 WebService 方法返回 bytesImage:

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

    在 J2ME 客户端上,您将从 URL 中读取响应,servlet 绑定到该 URL 并根据数据创建图像:

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

    希望这会有所帮助!

    【讨论】:

    • 非常感谢!这真的很有帮助。关于你的回答,我唯一要说的是:byte[] buffer = new byte[(int)conn.getLength()].
    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多