All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gnss.common.utils.CommonUtil Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
package com.gnss.common.utils;

import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.lang3.StringUtils;

import java.net.InetSocketAddress;

/**
 * 

Description: 工具类

*

Company: www.gps-pro.cn

* * @author huangguangbin * @version 1.0.1 * @date 2018/9/16 */ public class CommonUtil { private CommonUtil() { } /** * 格式化消息ID(转成0xXXXX) * * @param msgId 消息ID * @return 格式化字符串 */ public static String formatMessageId(int msgId) { return toHexFormat(msgId, 4); } /** * 格式化数字 * * @param num 数字 * @param size 长度 * @return 格式化字符串 */ public static String toHexFormat(int num, int size) { return "0x" + hexStr(num, size); } /** * 转4位的十六进制字符串 * * @param num 数字 * @return 格式化字符串 */ public static String hexStr(int num) { return hexStr(num, 4); } /** * 转十六进制字符串 * * @param num 数字 * @param size 长度 * @return 格式化字符串 */ public static String hexStr(int num, int size) { return StringUtils.leftPad(Integer.toHexString(num).toUpperCase(), size, '0'); } /** * 获取客户端IP * * @param ctx ChannelHandlerContext * @return 返回IP */ public static String getClientIp(ChannelHandlerContext ctx) { InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress(); return insocket.getAddress().getHostAddress(); } /** * 获取客户端端口 * * @param ctx ChannelHandlerContext * @return 返回端口 */ public static int getClientPort(ChannelHandlerContext ctx) { InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress(); return insocket.getPort(); } /** * 获取客户端IP信息 * * @param ctx ChannelHandlerContext * @return 返回IP */ public static String getClientAddress(ChannelHandlerContext ctx) { InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress(); return String.format("%s:%d", insocket.getAddress().getHostAddress(), insocket.getPort()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy