Hi, here is the code in the (non rpc) plain vanilla servlet, which
sends the image to the client.

I upload the image with a FileUpload Widget. On the server side, I
have a variation of the Apache commons FileUpload servet. This puts
the bytes in the database (with Hibernate, in my case) This generates
a primary key. This I pass back to the client with

response.setHeader("id", id);


And then the client calls a servlet ImageServlet like this:


public void setTargetImage() {
                // hack to prevent browser caching.
                Long timestamp = System.currentTimeMillis();
                String s = "?ts=" + timestamp.toString();
                String theBytes = GWT.getHostPageBaseURL() +
"ImageFromUploadServlet" + s;
                targetImage = new Image(theBytes);
                targetPanel.clear();
                targetPanel.add(targetImage);
}



public class ImageFromUploadServlet extends
javax.servlet.http.HttpServlet {

        private static final long serialVersionUID = 1L;

        private static Logger logger = Logger
        .getLogger("ac.otago.flags.server.ImageFromUploadServlet");

        public void init() throws ServletException {

        }

        /**
         * Gets the image. If a user (http session) has uploaded many images,
this
         * method will retrieve the latest one.
         *
         */
        protected void doGet(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, 
IOException
{
                String ID = request.getSession().getId();
                Provider provider = new Provider();
                // get all uploads for this user (session)
                Upload[] uploads = provider.getUploadsByUserid(ID);
                // get the most recent one
                Upload latestUpload = uploads[0];
                // logging
                logger.debug("user " + ID + " has uploaded " + uploads.length + 
"
files" );
                // get the image as bytes
                byte[] image = latestUpload.getImage();
                response.setContentType("image/jpeg");
                // stream out the bytes
                BufferedOutputStream bout = new BufferedOutputStream(response
                                .getOutputStream());
                for (int i = 0; i < image.length; i++) {
                        bout.write(image[i]);
                }
                bout.flush();
                bout.close();
                logger.debug("streaming image completed");
        }

        protected void doPost(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, 
IOException
{
                doGet(request, response);
        }

}


I hope this helps. Let me know if you need more information.

With kind regards from New Zealand,


Michel
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to