netty发送接收16进制代码_Netty之二进制文件传输

本文展示了如何使用Netty框架在客户端与服务器之间进行二进制文件的传输。通过创建Bootstrap实例,配置NioEventLoopGroup,设置ChannelInitializer来添加StringEncoder和ObjectDecoder处理文本和二进制数据。在客户端,将文件路径以JSON格式发送给服务器,服务器响应时将二进制文件内容返回,客户端将其保存到本地。整个过程实现了文件的高效、稳定传输。
摘要由CSDN通过智能技术生成

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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值