java获取本地IP和服务器IP
来源:互联网 发布:计算机三级软件测试 编辑:程序博客网 时间:2024/05/14 02:20
java获取IP地址
最近公司在做日志报警服务,并将异常信息推送到简聊,需要在异常信息上添加IP地址,用于快速定位异常位置。总结以下知识点:
- 获取本地IP地址
String address = InetAddress.getLocalHost().getHostAddress().toString();
这种方法能不能在Linux服务器上直接获取IP有待验证
- 获取服务器IP地址
/** * 设置异常信息 * @param t 异常信息。注意:这里使用的是Throwable来接收参数,为什么不是使用Exception? * 因为使用Exception接收的话,不好获取具体的异常信息,所以这里使用Throwable来接收,然后再 * 通过流的形式获取具体的异常信息 */ public void setMsg(Throwable t) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); t.printStackTrace(new PrintStream(stream)); String exceptionMsg = stream.toString(); log.error(exceptionMsg); // 记录到本地日志 JSONObject obj = new JSONObject(); obj.element("authorName", "异常报警"); obj.element("title", "IP为" + getServerIp() + "的又TM异常啦:"); obj.element("text", exceptionMsg); post(obj); } /** * 推送异常日志信息, * @param json 请求参数是用 net.sf.json.JSONObject 封装 */ public void post(JSONObject json) { HttpClient client; HttpPost post; try { client = HttpClients.createDefault(); post = new HttpPost(logServer); // logServer 是简聊的聚合服务地址 post.setHeader("Content-Type", "application/json"); post.addHeader("Authorization", "Basic YWRtaW46"); StringEntity s = new StringEntity(json.toString(), "utf-8"); s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(s); // 发送请求 HttpResponse httpResponse = client.execute(post); if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { log.info("请求服务器失败,请检查推送服务器"); } } catch (Exception e) { log.error("推送日志异常信息失败"); e.printStackTrace(); } } /** * 获取服务器地址 * * @return Ip地址 */ public String getServerIp() { // 获取操作系统类型 String sysType = System.getProperties().getProperty("os.name"); String ip; if (sysType.toLowerCase().startsWith("win")) { // 如果是Windows系统,获取本地IP地址 String localIP = null; try { localIP = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { log.error(e.getMessage(), e); } if (localIP != null) { return localIP; } } else { ip = getIpByEthNum("eth0"); // 兼容Linux if (ip != null) { return ip; } } return "获取服务器IP错误"; } /** * 根据网络接口获取IP地址 * @param ethNum 网络接口名,Linux下是eth0 * @return */ private String getIpByEthNum(String ethNum) { try { Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip; while (allNetInterfaces.hasMoreElements()) { NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement(); if (ethNum.equals(netInterface.getName())) { Enumeration addresses = netInterface.getInetAddresses(); while (addresses.hasMoreElements()) { ip = (InetAddress) addresses.nextElement(); if (ip != null && ip instanceof Inet4Address) { return ip.getHostAddress(); } } } } } catch (SocketException e) { logger.error(e.getMessage(), e); } return "获取服务器IP错误"; }
第一次写博客,第一次用MarkDown,太多的第一次,加油!
阅读全文
0 0
- java获取本地IP和服务器IP
- Java获取本地服务器IP
- Java获取本地服务器Ip的方法
- java获取本地ip
- java获取本地ip
- JAVA获取本地IP和PORT
- Java获取本地IP和计算机名
- 服务器获取本地Ip地址
- java 获得本地IP和远程服务器IP
- 一、java获取公网ip和本地ip
- Java获取本机ip和服务器ip
- 怎么获取本地IP 和远端IP
- 关于Java获取本地服务器IP的问题
- 获取服务器IP和获取访问者IP
- JAVA获取本地IP地址
- java 获取本地ip 主机名
- 用java获取本地ip
- Java获取本地IP地址
- mysql 5.7密码策略
- Shell函数FTP传送文件,并判断传送是否成功
- Surprising Strings<map集合使用>
- C语言 static关键字
- RabbitMQ 消息轮询和消息确认机制
- java获取本地IP和服务器IP
- mysql增删改查 常用脚本
- 常见数据结构的查找、插入、删除时间复杂度
- Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'
- bootstrap modal调用codemirror探索实践
- PaxCompiler脚本编译的一些要注意的问题
- localStorage记住用户密码
- Android ProgressBar的样式
- 不容易系列之二