如何使用GWT在客户端处理image / gif类型响应

时间:2009-11-19 12:20:25

标签: jsp gwt

我有一个关于如何在客户端处理image / gif类型响应的问题,任何建议都会很棒。 有一个服务响应从数据库检索图像(此时每次只有一个)。代码类似于

    JDBC Connection
    Construct MYSQL query. 
    Execute query
    If has ResultSet, retrieve first one {
    //save image into Blob image, “img” is the only entity in the image table.
    image = rs.getBlob("img"); 
} 

    response.setContentType("image/gif");   //set response type
    InputStream in = image.getBinaryStream();   //output Blob image to InputStream
    int bufferSize = 1024;  //buffer size
    byte[] buffer = new byte[bufferSize];       //initial buffer
    int length =0;
    //read length data from inputstream and store into buffer   
    while ((length = in.read(buffer)) != -1) {
        out.write(buffer, 0, length);       //write into ServletOutputStream 
    }
    in.close();
    out.flush(); //write out

客户端的代码

    ....
    imgform.setAction(GWT.getModuleBaseURL() + "serviceexample/ImgRetrieve"); 
    ....
    ClickListener {
        OnClick, then imgform.submit();
    }


    formHandler {

    onSubmit, form validation

    onSubmitComplete
        ??????? //handle response, and display image
            **Here is my question, i had tried 
            Image img = new Image(GWT.getHostPageBaseURL() +"serviceexample/ImgRetrieve");
            mg.setSize("300", "300");
            imgpanel.add(img);
            but i only got a non-displayed image with 300X300 size.**
    }

那么,在这种情况下我该如何处理响应?

谢谢,

1 个答案:

答案 0 :(得分:1)

您的问题非常模糊......我看到旧学校HTML表单/提交和GWT代码的组合。

常规GWT应用程序只需要在UI中的某个位置放置一个Image小部件,该小部件将引用您所描述的图像服务的URL。

显示图像的方式或时间取决于您。 但是,您需要做的就是在浏览器中显示图像

Image img = new Image( [url to image service] );
panel.add( img );

其中,面板是已经显示的某个面板。

如果您希望等到显示图像,直到用户点击某个按钮,请执行以下操作:

button.addClickHandler( new ClickHandler() {
  public void onClick( ClickEvent e ) {
    Image img = new Image( [url to image service] );
    panel.add( img );
  }
});
相关问题