java - 如何使用 Gwt 将生成的图像从服务器获取到客户端

标签 java image gwt client

我正在使用 gwt 开发一个 Web 应用程序,我在服务器端生成一个 BufferedImg,我想将其发送到客户端。我不知道该怎么做...

最佳答案

您可以在服务器端将图像生成为 BufferedImage,然后使用 Remote Procedure Calls 将其传递到客户端。 .

您必须实现:

  • 服务器端

    1. ImageServiceImpl 类扩展 RemoteServiceServlet在你的服务器端。 该类将生成一个 BufferedImage:
      文件图像路径 = new File(url); BufferedImage 图像 = ImageIO.read(imagePath); 您将将此图像发送到您的客户端。
  • 客户端

    1. 在客户端,您必须创建一个同步ImageService 接口(interface),它扩展 RemoteService接口(interface)。

    2. 您还必须基于原始服务接口(interface)创建另一个客户端接口(interface),即异步接口(interface) (ImageServiceAsync)。如 GWT 项目中所述

      The nature of asynchronous method calls requires the caller to pass in a callback object that can be notified when an asynchronous call completes, since by definition the caller cannot be blocked until the call completes. For the same reason, asynchronous methods do not have return types; they generally return void. Should you wish to have more control over the state of a pending request, return Request instead. After an asynchronous call is made, all communication back to the caller is via the passed-in callback object.

最后将您的 servlet 添加到您的 web.xml

    <servlet>
  <servlet-name>ImageServiceImpl</servlet-name>
  <servlet-class>
    com.example.image.server.ImageServiceImpl
  </servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>ImageServiceImpl</servlet-name>
  <url-pattern>/com.example.image.Image/ImageService</url-pattern>
</servlet-mapping>

为了将图像从服务器发送到客户端,您必须遵循 3 个步骤:

  1. 使用 GWT.create() 实例化服务接口(interface)。

  2. 创建一个异步回调对象,以便在 RPC 完成时收到通知。

  3. 调用电话。

这些是要遵循的通用规则,我建议这不是您问题的最终解决方案,而是作为指导,以便了解 RPC 是如何定义和工作的。 RPC 一开始有点复杂,但是一旦您了解了其机制,您将能够将各种数据从服务器传递到客户端。

[编辑]

对于图像类型,请记住,在 GWT 中,HTML 图像是通过 IMG 元素中的 SRC 属性加载的。通过 RPCT 将图像从服务器传递到客户端不能直接完成,而是使用技巧:

  1. 让 RPC 调用返回 Base 64 格式的图像字符串。
  2. 在 onSuccess() 方法中,使用该结果字符串作为 setUrl() 的参数(哪个 设置 SRC 属性)。

关于java - 如何使用 Gwt 将生成的图像从服务器获取到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19695539/

相关文章:

java - Netbeans 7.2 无法在 OS X 上打开

html - CSS - 链接中的文本和图像与文本垂直居中对齐

javascript - 预加载图像并具有缓冲区符号

gwt - "Pretty time"用于 GWT

java - 在 Java 中查找特定长度/格式的子字符串

java - 套接字异常 : TOO MANY OPEN FILES

java - 在Java中获取日期范围内的所有星期五

c++ - 检查一张图片是否是另一张图片的移位

java - IE 中的 GWT 性能问题

java - 使用 GWT 上传前检查文件大小