如何使用servlet获取图像并使用GWT图像类显示它? [英] How to get image with servlet and display it using GWT Image class?

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

问题描述

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

  private void getImage(){
HttpServletResponse res = this.getThreadLocalResponse();
尝试{
//设置内容类型
res.setContentType(image / png);

//设置内容大小
文件文件=新文件(C:\\Documents and Settings\\User\\image.png);
res.setContentLength((int)file.length());

//打开文件并输出流
FileInputStream in = new FileInputStream(file);
OutputStream out = res.getOutputStream();

//将文件内容复制到输出流
byte [] buf = new byte [1024];
int count = 0; ((count = in.read(buf))> = 0){
out.write(buf,0,count);
}
in.close();
out.close();
} catch(IOException e){
e.printStackTrace();


$ / code>

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

Servlets响应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)
抛出IOException {

//你的图像servlet代码在这里
resp.setContentType(image / jpeg);

//设置内容大小
文件file = new File(path / to / image.jpg);
resp.setContentLength((int)file.length());

//打开文件并输出流
FileInputStream in = new FileInputStream(file);
OutputStream out = resp.getOutputStream();

//将文件内容复制到输出流
byte [] buf = new byte [1024];
int count = 0; ((count = in.read(buf))> = 0){
out.write(buf,0,count);
}
in.close();
out.close();


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

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

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


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

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.

解决方案

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.

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

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>

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆