本文介绍了如何使用 servlet 获取图像并使用 GWT Image 类显示它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下代码用作 GWT-RPC 的 GWT 服务器端类 (servlet) 的一部分.

I'm using the following code as part of the GWT server-side class (servlet) for GWT-RPC.

private void getImage() {
        HttpServletResponse res = this.getThreadLocalResponse();
        try {
            // Set content type
            res.setContentType("image/png");

            // Set content size
            File file = new File("C:\Documents and Settings\User\image.png");
            res.setContentLength((int) file.length());

            // Open the file and output streams
            FileInputStream in = new FileInputStream(file);
            OutputStream out = res.getOutputStream();

            // Copy the contents of the file to the output stream
            byte[] buf = new byte[1024];
            int count = 0;
            while ((count = in.read(buf)) >= 0) {
                out.write(buf, 0, count);
            }
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

当我按下客户端上的按钮时,servlet 正在运行.我想使用 Image 类将图像加载到客户端,但我不知道如何从 servlet 获取图像的 url 到客户端的代码以显示它.这是正确的程序还是有其他方法?我使用 GWT 作为客户端,使用 GWT-RPC 进行客户端-服务器通信.

The servlet is running when i press a button on the client. I want to use the Image class to load the image into the client but i do not know how to get the url of the image from the servlet to the client's code in order to display it. Is this correct procedure or there is another way? I use GWT for the client and GWT-RPC for client-server communication.

推荐答案

Servlet 响应各种 HTTP 方法:GET、POST、PUT、HEAD.由于您使用 GWT 的 new Image(url),并且它使用 GET,因此您需要有一个处理 GET 方法的 servlet.

Servlets respond to varios HTTP methods: GET, POST, PUT, HEAD. Since you use GWT's new Image(url), and it uses GET, you need to have a servlet that handles GET method.

为了让 servlet 处理 GET 方法,它必须覆盖 HttpServlet 的 doGet(..) 方法.

In order for servlet to handle GET method it must override a doGet(..) method of HttpServlet.

public class ImageServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {

        //your image servlet code here
        resp.setContentType("image/jpeg");

        // Set content size
        File file = new File("path/to/image.jpg");
        resp.setContentLength((int)file.length());

        // Open the file and output streams
        FileInputStream in = new FileInputStream(file);
        OutputStream out = resp.getOutputStream();

        // Copy the contents of the file to the output stream
        byte[] buf = new byte[1024];
        int count = 0;
        while ((count = in.read(buf)) >= 0) {
            out.write(buf, 0, count);
        }
        in.close();
        out.close();
    }
}

然后你必须在你的 web.xml 文件中配置你的 servlet 的路径:

Then you must configure path to your servlet in your web.xml file:

<servlet>
    <servlet-name>MyImageServlet</servlet-name>
    <servlet-class>com.yourpackage.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyImageServlet</servlet-name>
    <url-pattern>/images</url-pattern>
</servlet-mapping>

然后在 GWT 中调用它:new Image("http:yourhost.com/images")

Then call it in GWT: new Image("http:yourhost.com/images")

这篇关于如何使用 servlet 获取图像并使用 GWT Image 类显示它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:09