users@grizzly.java.net

Re: Grizzly fileupload

From: Oleksiy Stashok <oleksiy.stashok_at_oracle.com>
Date: Mon, 14 Mar 2011 12:37:48 +0100

Hi Gagan,

sorry for the late response.
Let's start from your first email.

Regarding Glassfish, there seems to be an issue [1], when maxPostSize
settings is ignored, so looks like only chunked Transfer-Encoding will
work for large files upload.

As for Grizzly, in Grizzly 2.0.1 we want to simplify HttpHandler API a
bit, so we'd recommend you to use Grizzly 2.0.1-b2 build (for now, the
release will happen by the end of this week), and here is the sample [2]
code to handle file uploads in non-blocking way. The sample, actually,
is not saving request content to a file, but just "echoes" it, though it
should help to get an idea.

Hope that would help.

If you have more questions - please ask.

WBR,
Alexey.

[1] http://java.net/jira/browse/GLASSFISH-16194
[2]
     /**
      * This handler using non-blocking streams to read POST data and
echo it
      * back to the client.
      */
     private static class NonBlockingEchoHandler extends HttpHandler {


         // -------------------------------------------- Methods from
HttpHandler


         @Override
         public void service(final Request request,
                             final Response response) throws Exception {

             final char[] buf = new char[128];
             final NIOReader in = request.getReader(false); // false
argument puts the stream in non-blocking mode
             final NIOWriter out = response.getWriter();

             response.suspend();

             // If we don't have more data to read - onAllDataRead()
will be called
             in.notifyAvailable(new ReadHandler() {

                 @Override
                 public void onDataAvailable() throws IOException {
                     System.out.println("[onDataAvailable] length: " +
in.readyData());
                     in.notifyAvailable(this);
                 }

                 @Override
                 public void onError(Throwable t) {
                     System.out.println("[onError]" + t);
                 }

                 @Override
                 public void onAllDataRead() throws IOException {
                     System.out.println("[onAllDataRead] length: " +
in.readyData());
                     try {
                         echoAvailableData(in, out, buf);
                     } finally {
                         try {
                             in.close();
                         } catch (IOException ignored) {
                         }

                         try {
                             out.close();
                         } catch (IOException ignored) {
                         }

                         response.resume();
                     }
                 }
             });

         }

         private void echoAvailableData(NIOReader in, NIOWriter out,
char[] buf)
                 throws IOException {

             while(in.isReady()) {
                 int len = in.read(buf);
                 out.write(buf, 0, len);
             }
         }

     } // END NonBlockingEchoHandler

On 03/14/2011 07:44 AM, gagansnt wrote:
> I am working on a server application using Grizzly which can handle uploading
> of very large files. I am looking for a sample example which shows how to
> parse a POST request and save file on server side.
>
> If anybody had written such example then I would appreciate if you could
> share it.
>
> Thanks in advance.
> Gagan