1 packagetest;2
3 importcom.fr.general.IOUtils;4 importcom.fr.json.JSONException;5 importcom.fr.json.JSONObject;6 importcom.fr.stable.core.UUID;7 importio.netty.bootstrap.Bootstrap;8 importio.netty.channel.ChannelFuture;9 importio.netty.channel.ChannelHandlerContext;10 importio.netty.channel.ChannelInboundHandlerAdapter;11 importio.netty.channel.ChannelInitializer;12 importio.netty.channel.ChannelPipeline;13 importio.netty.channel.EventLoopGroup;14 importio.netty.channel.nio.NioEventLoopGroup;15 importio.netty.channel.socket.SocketChannel;16 importio.netty.channel.socket.nio.NioSocketChannel;17 importio.netty.handler.codec.serialization.ClassResolvers;18 importio.netty.handler.codec.serialization.ObjectDecoder;19 importio.netty.handler.codec.string.StringEncoder;20
21 importjava.io.ByteArrayInputStream;22 importjava.io.File;23 importjava.io.FileNotFoundException;24 importjava.io.FileOutputStream;25 importjava.io.IOException;26
27 public classFileClient {28
29 publicFileClient(){30
31 }32
33 public voidstart() {34 EventLoopGroup group = newNioEventLoopGroup();35 try{36 Bootstrap bootstrap = newBootstrap();37 bootstrap.group(group)38 .channel(NioSocketChannel.class)39 .handler(new ChannelInitializer() {40
41 @Override42 protected void initChannel(SocketChannel s) throwsException {43 ChannelPipeline p =s.pipeline();44 //传输文本给服务器端
45 p.addLast(newStringEncoder());46 //二进制文件获取解析
47 p.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(this
48 .getClass().getClassLoader())));49 //客户端业务代码
50 p.addLast(newFileClientHandler());51 }52 });53 ChannelFuture future = bootstrap.connect("localhost", 7766).sync();54 future.channel().closeFuture().sync();55 } catch(InterruptedException e) {56 e.printStackTrace();57 } finally{58 group.shutdownGracefully();59 }60 }61
62 public static void main(String[] args) throwsInterruptedException {63 newFileClient().start();64 }65
66 private static class FileClientHandler extendsChannelInboundHandlerAdapter {67
68
69 @Override70 public voidchannelActive(ChannelHandlerContext ctx) {71 try{72 //将要获取的pdf路径发送给服务器端
73 JSONObject jo = JSONObject.create().put("path", "d:\\a.pdf");74 ctx.writeAndFlush(jo.toString());75 } catch(JSONException e) {76 e.printStackTrace();77 }78 }79
80 @Override81 public voidchannelRead(ChannelHandlerContext ctx, Object msg) {82 PDFContent content =(PDFContent) msg;83 //从服务器端获取的二进制文件存到本地
84 String fileName = UUID.randomUUID().toString() + ".pdf";85 File file = new File("D:\\" +fileName);86 try{87 FileOutputStream out = newFileOutputStream(file);88 IOUtils.copyBinaryTo(newByteArrayInputStream(content.getContent()), out);89 out.close();90 } catch(FileNotFoundException e) {91 e.printStackTrace();92 } catch(IOException e) {93 e.printStackTrace();94 }95 try{96 JSONObject jo = JSONObject.create().put("res", "Thank You, I Have The File!");97 ctx.writeAndFlush(jo.toString());98 ctx.close();99 } catch(JSONException e) {100 e.printStackTrace();101 }102 }103
104 @Override105 public voidexceptionCaught(ChannelHandlerContext ctx, Throwable cause) {106 cause.printStackTrace();107 ctx.close();108 }109 }110 }