Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用servlet获取图像并使用GWT图像类显示它?_Gwt_Gwt Rpc_Gwt2 - Fatal编程技术网

如何使用servlet获取图像并使用GWT图像类显示它?

如何使用servlet获取图像并使用GWT图像类显示它?,gwt,gwt-rpc,gwt2,Gwt,Gwt Rpc,Gwt2,我使用以下代码作为GWT-RPC的GWT服务器端类(servlet)的一部分 private void getImage() { HttpServletResponse res = this.getThreadLocalResponse(); try { // Set content type res.setContentType("image/png"); // Set content s

我使用以下代码作为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用于客户端-服务器通信。

servlet响应varios HTTP方法:GET、POST、PUT、HEAD。由于您使用GWT的
新图像(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>

MyImageServlet
com.yourpackage.ImageServlet
MyImageServlet
/图像

然后在GWT中调用它:
newimage(“http:yourhost.com/images”)

如果我有一组图像,它们可以像您的示例中那样由单个servlet显示吗?是的,您需要向servlet传递一个参数:
/images?name=something
然后您可以通过
字符串param=req.getParameter(“name”)在servlet中获取参数
要稍微澄清这一点:每个请求只能提供一个图像。因此,要同时显示多个图像,必须提出多个请求(如果要显示文件夹的所有图像)