实验的源文件是一个图片,假设地址是D:\Koala.jpg,接收保存后的图片为D:\test.jpg
原理就是将文件读取成byte,通过bytebuffer发送即可
客户端
package net.xjdsz.file;import io.netty.bootstrap.Bootstrap;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.*;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioSocketChannel;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.InputStream;/*** Created by dingshuo on 2017/7/6.*/public class UploadClient {public static void main(String[] args) throws Exception{UploadClient client=new UploadClient();client.connect();}public void connect(){EventLoopGroup group=new NioEventLoopGroup();try{Bootstrap b=new Bootstrap();b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY,true).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {ByteBuf msg;InputStream in = new FileInputStream("D:\Koala.jpg");ByteArrayOutputStream out = new ByteArrayOutputStream();byte[] buffer = new byte[1024 * 100];int n = 0;while ((n = in.read(buffer)) != -1) {msg= Unpooled.buffer(buffer.length);//这里读取到多少,就发送多少,是为了防止最后一次读取没法满填充buffer,//导致将buffer中的处于尾部的上一次遗留数据也发送走msg.writeBytes(buffer,0,n);ctx.writeAndFlush(msg);msg.clear();}System.out.println(n);}});}});ChannelFuture f=b.connect("127.0.0.1",20000).sync();f.channel().closeFuture().sync();}catch (Exception e){}finally {group.shutdownGracefully();}}}
服务端
package net.xjdsz.file;import io.netty.bootstrap.ServerBootstrap;import io.netty.buffer.ByteBuf;import io.netty.channel.*;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.logging.LogLevel;import io.netty.handler.logging.LoggingHandler;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;/*** Created by dingshuo on 2017/7/6.*/public class UploadServer {public void bind(int port) throws Exception{EventLoopGroup bossGroup=new NioEventLoopGroup();EventLoopGroup workerGroup=new NioEventLoopGroup();try{ServerBootstrap b=new ServerBootstrap();b.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG,1024).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {super.channelActive(ctx);}@Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {super.channelInactive(ctx);}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {String path="D:\test.jpg";File file=new File(path);if(!file.exists()){file.createNewFile();}FileOutputStream fos=new FileOutputStream(file,true);// BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fos);ByteBuf buf=(ByteBuf)msg;byte[] bytes=new byte[buf.readableBytes()];buf.readBytes(bytes);System.out.println("本次接收内容长度:" + bytes.length);try {// bufferedOutputStream.write(bytes, 0, bytes.length);// buf.release();fos.write(bytes);fos.flush();} catch (IOException e) {e.printStackTrace();}}});}});//绑定端口,同步等待成功ChannelFuture f=b.bind(port).sync();//等待服务端监听端口关闭f.channel().closeFuture().sync();}finally {//退出,释放资源bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}public static void main(String[] args) throws Exception{UploadServer uploadServer=new UploadServer();uploadServer.bind(20000);}}