How to handle image/gif type response on client side using GWT

Posted by user200340 on Stack Overflow See other posts from Stack Overflow or by user200340
Published on 2009-11-19T12:20:25Z Indexed on 2010/04/30 8:27 UTC
Read the original article Hit count: 238

Filed under:
|

Hi all,

I have a question about how to handle image/gif type response on client side, any suggestion will be great. There is a service which responds for retrieving image (only one each time at the moment) from database. The code is something like,

    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

The code on client side

    ....
    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.**
    }

So, how should i handle the responde in this case?

Thanks,

© Stack Overflow or respective owner

Related posts about gwt

Related posts about jsp