netty发送utf-8编码的信息

时间:2021-05-07 19:16:14
/**
*
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception { // (1) //发送告警信息
List<FaultAlarm> faultsAlarms=ReadWarnInfo.getFaultAlarms();
StringBuilder sb=new StringBuilder();
for(FaultAlarm fa : faultsAlarms){ sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
sb.append("<Tel_Alarm>");
sb.append("<actionName name=\"createAlarmInfo\">");
sb.append("<param name=\"systemCode\">2</param>");
sb.append("<param name=\"stationCode\">4</param>");
sb.append("<param name=\"deviceSN\"> DEVICE004</param>");
sb.append("<param name=\"deviceType\">DT1</param>");
sb.append("<param name=\"deviceName\">qqA</param>");
sb.append("<param name=\"alarmUnitSN\">002</param>");
sb.append("<param name=\"alarmUnitType\">13</param>");
sb.append("<param name=\"alarmUnitName\">qqb</param>");
sb.append("<param name=\"alarmInfoName\">USER REQ SHUTDOWN</param>");
sb.append("<param name=\"reason\">测试系统故障</param>");
sb.append("<param name=\"alarmCode\">1</param>");
sb.append("<param name=\"happenedTime\">1300938099921</param>");
sb.append("</actionName>");
sb.append("</Tel_Alarm>");
sb.append("</xml>");
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);
} }