some example to send big file from server to client?

tantom tantom2000 at gmail.com
Thu May 21 16:31:25 EDT 2009


hi
  Thanks!Trustin and frederic,i will try yours suggest.

i try Frederic's tips, code below, it always print "wait" in the server
side.?
what's wrong with below code?  when i remove the "while isWritable" fragment
it runs ok.
-----------------------------------------------------------------------
                FileInputStream fis = new FileInputStream(f);
                byte[] bs = new byte[1024];
                while (fis.read(bs) > 0) {
                     ChannelBuffer buffer = ChannelBuffers.copiedBuffer(bs); 
                    while (!channel.isWritable()) {
                        System.out.println("wait");
                        Thread.sleep(5000);
                    }                   
                    channel.write(buffer);
                }
                fis.close();



Trustin Lee-2 wrote:
> 
> Actually you can achieve more reliable and faster transfer using
> ChannelFutureListener.  The following is the code snippet I used when
> I write an HTTP-based file server:
> 
>     ChannelFuture writeFuture = e.getChannel().write(httpResponseHeader);
>     final long fileLength = file.length();
>     final FileInputStream fis = new FileInputStream(file);
>     writeFuture.addListener(new ChannelFutureListener() {
>         private final ChannelBuffer buffer = ChannelBuffers.buffer(4096);
>         private long offset = 0;
> 
>         public void operationComplete(ChannelFuture future)
>                 throws Exception {
>             if (!future.isSuccess()) {
>                 future.getCause().printStackTrace();
>                 future.getChannel().close();
>                 fis.close();
>                 return;
>             }
> 
>             System.out.println("SENDING: " + offset + " / " + fileLength);
>             buffer.clear();
>             buffer.writeBytes(fis, (int) Math.min(fileLength - offset,
>                     buffer.writableBytes()));
>             offset += buffer.writerIndex();
>             ChannelFuture chunkWriteFuture =
>                     future.getChannel().write(buffer);
>             if (offset < fileLength) {
>                 // Send the next chunk
>                 chunkWriteFuture.addListener(this);
>             } else {
>                 // Wrote the last chunk - close the connection if the
> write is done.
>                 System.out.println("DONE: " + fileLength);
>                 chunkWriteFuture.addListener(ChannelFutureListener.CLOSE);
>                 fis.close();
>             }
>         }
>     });
> 
> — Trustin Lee, http://gleamynode.net/
> 
> 
> 
> On Fri, May 22, 2009 at 3:48 AM, Frederic Bregier <fredbregier at free.fr>
> wrote:
>>
>> Hi,
>>
>> I think that you can even improve it by taking care of write ability like
>> "discard" example shows. Indeed, in your code, if the network link is
>> slow,
>> you can have the side effect in the client to fill the write queue so
>> much
>> that there will be an OOME.
>>
>> A simple implementation idea could be as follow (from your code example):
>>
>>                FileInputStream fis = new FileInputStream(f);
>>                int len;
>>                byte[] bs = new byte[1024];
>>                while ((len = fis.read(bs)) > 0) {
>>                    ChannelBuffer buffer =
>> ChannelBuffers.copiedBuffer(bs);
>>                    // directly create and copy the ChannelBuffer from the
>> byte array
>>                    while (! channel.isWritable()) {
>>                         Thread.sleep(10); // wait a while (maybe more
>> than
>> 10ms)
>>                         // until channel is writable again
>>                    }
>>                    channel.write(buffer);
>>                }
>>                fis.close();
>>
>> Of course, if you have a client Handler, then you can directly code this
>> in
>> the handler such that you can even prevent your code of this active wait
>> loop (see Discard example to see how).
>>
>> HTH too,
>> Frederic
>>
>>
>> tantom wrote:
>>>
>>> hi
>>>
>>> thanks,it's ok. below it's my test code,i post it hope to help next who
>>> encount same with me:)
>>>
>>>  File f = new File("g:\\test.msi");
>>>                 String params = "GetFile|test.msi";
>>>                 byte[] bsParams = params.getBytes("UTF-8");
>>>                 ChannelBuffer response = ChannelBuffers.buffer(8 +
>>> bsParams.length);
>>>
>>> response.writeBytes(CTypeConvert.int2byte(bsParams.length));
>>>                 response.writeBytes(CTypeConvert.int2byte((int)
>>> f.length()));
>>>                 response.writeBytes(bsParams);
>>>
>>>                 //write the header info first
>>>                 channel.write(response);
>>>
>>>
>>>                 FileInputStream fis = new FileInputStream(f);
>>>                 int len;
>>>                 byte[] bs = new byte[1024];
>>>                 while ((len = fis.read(bs)) > 0) {
>>>                     ChannelBuffer buffer = ChannelBuffers.buffer(len);
>>>                     buffer.writeBytes(bs);
>>>                     channel.write(buffer);
>>>                 }
>>>                 fis.close();
>>>
>>
>>
>> -----
>> Hardware/Software Architect
>> --
>> View this message in context:
>> http://n2.nabble.com/some-example-to-send-big-file-from-server-to-client--tp2952981p2953620.html
>> Sent from the Netty User Group mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> netty-users mailing list
>> netty-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/netty-users
>>
> 
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users
> 
> 

-- 
View this message in context: http://n2.nabble.com/some-example-to-send-big-file-from-server-to-client--tp2952981p2954039.html
Sent from the Netty User Group mailing list archive at Nabble.com.





More information about the netty-users mailing list