tags:

views:

36

answers:

1

Hello. Could anyone help me with java Framework - stripes? I try to upload image with stripes:file, resize on server and return with new StreamingResolution return ("image / jpeg"... I dont now exactly how send with StreamingResolution and how can I load image after stripes:file element on jsp?

Many Thanks

+2  A: 

Well if you want to show the image on the page after uploading, there are a couple of things to deal with. First, the "POST" is going to have to have some sort of result, and that result probably won't be the image data. That is, having your action (your "event handler", as Stripes people call them) return a StreamingResolution doesn't really make sense unless you just want the user to be able to save the image like a downloaded file.

Thus, your image upload "POST" might involve just a plain resolution that forwards to a result page. Inside that page, you can put an HTML <img> tag that has a "src" set to another Stripes action. Now that action will return the StreamingResolution for your image data.

One problem to solve is where to keep the image data across the two HTML transactions. For that, I'd use a Stripes "flash scope" because it's pretty simple. If your server code is going to store the image in a database anyway, of course, your image action URL would simply include some sort of identifying information.

Assuming you can find the image data, all your server-side handler has to do is to create a StreamingResolution instance that has an implementation for the stream() method. That takes a single parameter (the HttpServletResponse). From that, you'd open up an output stream with response.getOutputStream(), copy the image data out to that, and then close the stream. There's really not much to it. Here's an example that sends out a simple file, but for you the main difference would be keeping track of the image data and of course a different MIME type:

public Resolution image() {
    return new StreamingResolution("text/plain") {
        public void stream(final HttpServletResponse response) {
            InputStream sample = null;
            try {
                sample = getResourceAsStream(SAMPLE + getContext().getLocale().toString());
                if (sample == null) sample = getResourceAsStream(SAMPLE + "en_US");

                final OutputStream out = response.getOutputStream();
                final byte buffer[] = new byte[8192];

                out.write((HEADER + "\n").getBytes());

                for (int rc = sample.read(buffer); rc > 0; rc = sample.read(buffer))
                    out.write(buffer, 0, rc);
            }
            finally {
                if (sample != null) try { sample.close(); } catch (IOException ioe) { }
            }
        }
    };
}

You'd want to also call setAttachment(false); before you start writing out the image data. This example is for a file download, so in my case I want it to be an attachment (and that's the default). If you're responding to a "GET" generated from an <img> tag, however, you don't want it to look like an attachment.

Pointy