netty发送utf-8编码的信息

/**
     * 
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception { // (1)
        
        //发送告警信息
        List faultsAlarms=ReadWarnInfo.getFaultAlarms();
        StringBuilder sb=new StringBuilder();
        for(FaultAlarm fa : faultsAlarms){
            
            sb.append("");
            sb.append("");
            sb.append("");
            sb.append("2");
            sb.append("4");
            sb.append(" DEVICE004");
            sb.append("DT1");
            sb.append("qqA");
            sb.append("002");
            sb.append("13");
            sb.append("qqb");
            sb.append("USER REQ SHUTDOWN");
            sb.append("测试系统故障");
            sb.append("1");
            sb.append("1300938099921");
            sb.append("");
            sb.append("");
            sb.append("");
            sb.append("\n");
            
            ByteBuf bufwarn = ctx.alloc().buffer(3072);
            byte[]  warnsbytes=sb.toString().getBytes("UTF-8"); //只要这里设置一下就可以了!!!
            bufwarn.writeBytes(warnsbytes);
            ChannelFuture f= ctx.writeAndFlush(bufwarn);
            if(f.isSuccess()){
                System.out.println("告警报文发送成功!");
            }
            
            Thread.sleep(5000);
        }
        
    }

 

转载于:https://www.cnblogs.com/Anders888/p/5780514.html

你可能感兴趣的:(netty,java)