If you continue to focus on onecoder, you might ask, in the Java NIO Framework Netty Tutorial (14) Netty Oio model (contrast NIO) is not described in the next section, how do the worker processes in NiO and OIO. This is bound to happen, but in the course of the study, Onecoder found the previous legacy file transfer code, so decided to finish it first.
In fact, Netty sample code also provides the file upload download code sample, but too complex, also includes the parsing of HTTP requests, for Onecoder, it is easy to confuse those who are the key to file transfer. So Onecoder decided to write a sample according to his own, this understanding is at the beginning of the mention, Netty transmission is based on the flow, we have to stream the file should be able to pass. The following code is then available:
View Sourceprint?01./**02.* 文件传输接收端,没有处理文件发送结束关闭流的情景03.*04.* @author lihzh05.* @alia OneCoder06.* @blog http://www.coderli.com07.*/08.public class FileServerHandler extends SimpleChannelHandler {09. 10.private File file = new File("F:/2.txt");11.private FileOutputStream fos;12. 13.public FileServerHandler() {14.try {15.if (!file.exists()) {16.file.createNewFile();17.} else {18.file.delete();19.file.createNewFile();20.}21.fos = new FileOutputStream(file);22.} catch (IOException e) {23.e.printStackTrace();24.}25.}26. 27.@Override28.public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)29.throws Exception {30.ChannelBuffer buffer = (ChannelBuffer) e.getMessage();31.int length = buffer.readableBytes();32.buffer.readBytes(fos, length);33.fos.flush();34.buffer.clear();35.}36. 37.}
View Sourceprint?01./**02.* 文件发送客户端,通过字节流来发送文件,仅实现文件传输部分,<br>03.* 没有对文件传输结束进行处理<br>04.* 应该发送文件发送结束标识,供接受端关闭流。05.* www.it165.net06.* @author lihzh07.* @alia OneCoder08.* @blog http://www.coderli.com09.*/10.public class FileClientHandler extends SimpleChannelHandler {11. 12.// 每次处理的字节数13.private int readLength = 8;14. 15.@Override16.public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)17.throws Exception {18.// 发送文件19.sendFile(e.getChannel());20.}21. 22.private void sendFile(Channel channel) throws IOException {23.File file = new File("E:/1.txt");24.FileInputStream fis = new FileInputStream(file);25.int count = 0;26.for (;;) {27.BufferedInputStream bis = new BufferedInputStream(fis);28.byte[] bytes = new byte[readLength];29.int readNum = bis.read(bytes, 0, readLength);30.if (readNum == -1) {31.return;32.}33.sendToServer(bytes, channel, readNum);34.System.out.println("Send count: " + ++count);35.}36. 37.}38. 39.private void sendToServer(byte[] bytes, Channel channel, int length)40.throws IOException {41.ChannelBuffer buffer = ChannelBuffers.copiedBuffer(bytes, 0, length);42.channel.write(buffer);43.}44. 45.}
The contents of the file to be sent 1.txt are as follows:
Run the above code and accept the file 2.txt results:
Exactly alike. Success!
This is just a simple example of file transfer, which can be used as a reference for example. For large file transfer scenarios, this sample does not support a memory overflow scenario, and Onecoder is ready to be introduced separately.
Java NIO Framework Netty Tutorial (15)-File transfer with Netty