【问题标题】:How to get image with servlet and display it using GWT Image class?如何使用 servlet 获取图像并使用 GWT Image 类显示它?
【发布时间】:2011-06-27 09:39:57
【问题描述】:

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

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 类将图像加载到客户端,但我不知道如何将图像的 url 从 servlet 获取到客户端代码以显示它。这是正确的程序还是有其他方法?我使用 GWT 作为客户端,使用 GWT-RPC 进行客户端-服务器通信。

【问题讨论】:

    标签: gwt gwt-rpc gwt2


    【解决方案1】:

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

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

    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 的路径:

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

    【讨论】:

    • 如果我有一组图像,它们可以像您的示例中那样由单个 servlet 显示吗?
    • 是的,您需要向 servlet 传递一个参数:/images?name=something
    • 然后就可以通过String param = req.getParameter("name")获取servlet中的参数了
    • 澄清一下:每个请求只能提供一张图片。
    • 所以要同时显示多个图像,必须提出多个请求(如果要显示一个文件夹的所有图像)
    猜你喜欢
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多