Netty 5 ways to transfer large files

Source: Internet
Author: User

Netty 5 provides a chunkedwriterhandler to transfer large files, the sending end is as follows:

Add Chunedwriterhandler:

ChannelPipelineChunkedWriteHandler()); P.addlast ("Handler", New MyHandler ());

Send files directly
ChannelChunkedFile(New File ("video.mkv"));

It is important to note that Chunkedwriterhandler must be added in front of the handler that sent the file. This means that handler is added before the channel write Chunedfile can be called.

The code of the receiver is as follows, the key point is that the sending side needs to inform the file size (file.length), add a handler to all the handler front, so that the file content is blocked, the file will be removed handler.

Ctx.pipeline (). AddFirst (new Chunkedreadhandler (size));

1 classChunkedreadhandlerextendschannelhandleradapter{2     Private LongfileSize;3     Privatefile file;4     PrivateFileOutputStream ofs;5     Private LongReadedsize=0;6      PublicChunkedreadhandler (LongSizethrowsfilenotfoundexception{7          This. filesize=size;8          This. File =NewFile ("C:/2.zip"); 9ofs=NewFileOutputStream ( This. file);Ten     } One @Override A      Public voidChannelread (Channelhandlercontext ctx, Object msg)throwsException { -          -Bytebuf buf=(BYTEBUF) msg; the          -Readedsize + =buf.readablebytes (); -         if(Buf.isreadable ()) { -             byte[] bytes=New byte[Buf.readablebytes ()]; + buf.readbytes (bytes); - ofs.write (bytes); +         } A          atSystem.out.println (FileSize + "" +readedsize); -          -         if(Readedsize >=fileSize) { -Ctx.pipeline (). Remove ( This); - ofs.close (); -         } in buf.release (); -     } to}

The doc in the Netty5 document gives a picture that clearly describes the handler relationship, which is a hierarchical relationship like the TCP/IP protocol.

The general order of handler additions is:

    1. Protocol decoder-translates binary data (e.g. ByteBuf ) into a Java object.
    2. Protocol encoder-translates a Java object into binary data.
    3. Business logic handler-performs The actual business logic (e.g. database access).

The corresponding input and output layer is added first, and then the next layer of handler is added.

As a result, our logic layer works at the highest level, with the highest degree of abstraction and convenience.

Netty 5 ways to transfer large files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.