这篇文章将为大家详细讲解有关服务器的真实IP怎么利用java进行获取,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
实现代码:
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.Inet4Address;import java.net.InetAddress;import java.net.InterfaceAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.net.URL;import java.net.URLConnection;import java.net.URLEncoder;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.Enumeration;import java.util.Iterator;import java.util.List;import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.springframework.http.HttpMethod; public class WebToolUtils { public static String getLocalIP() throws UnknownHostException, SocketException { if (isWindowsOS()) { return InetAddress.getLocalHost().getHostAddress(); } else { return getLinuxLocalIp(); } } public static boolean isWindowsOS() { boolean isWindowsOS = false; String osName = System.getProperty("os.name"); if (osName.toLowerCase().indexOf("windows") > -1) { isWindowsOS = true; } return isWindowsOS; } public static String getLocalHostName() throws UnknownHostException { return InetAddress.getLocalHost().getHostName(); } private static String getLinuxLocalIp() throws SocketException { String ip = ""; try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); String name = intf.getName(); if (!name.contains("docker") && !name.contains("lo")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { String ipaddress = inetAddress.getHostAddress().toString(); if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) { ip = ipaddress; System.out.println(ipaddress); } } } } } } catch (SocketException ex) { System.out.println("获取ip地址异常"); ip = "127.0.0.1"; ex.printStackTrace(); } System.out.println("IP:"+ip); return ip; } public static String getIpAddress(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } // public static String sendGet(String url, String param) { // String result = ""; // BufferedReader in = null; // try { // String urlNameString = url + "?" + param; // URL realUrl = new URL(urlNameString); // // 打开和URL之间的连接 // URLConnection connection = realUrl.openConnection(); // // 设置通用的请求属性 // connection.setRequestProperty("accept", "* public static void sendPost(String pathUrl, String name, String pwd, String phone, String content) { // PrintWriter out = null; // BufferedReader in = null; // String result = ""; // try { // URL realUrl = new URL(url); // // 打开和URL之间的连接 // URLConnection conn = realUrl.openConnection(); // // 设置通用的请求属性 // conn.setRequestProperty("accept", "**;q=0.8"); httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate"); httpConn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"); httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0"); httpConn.setRequestProperty("Upgrade-Insecure-Requests", "1"); httpConn.setRequestProperty("account", name); httpConn.setRequestProperty("passwd", pwd); httpConn.setRequestProperty("phone", phone); httpConn.setRequestProperty("content", content); // 建立输出流,并写入数据 OutputStream outputStream = httpConn.getOutputStream(); outputStream.write(requestStringBytes); outputStream.close(); // 获得响应状态 int responseCode = httpConn.getResponseCode(); if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功 // 当正确响应时处理数据 StringBuffer sb = new StringBuffer(); String readLine; BufferedReader responseReader; // 处理响应流,必须与服务器响应流输出的编码一致 responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "utf-8")); while ((readLine = responseReader.readLine()) != null) { sb.append(readLine).append("\n"); } responseReader.close(); } } catch (Exception ex) { ex.printStackTrace(); } } public static void doPost(String url, String name, String pwd, String phone, String content) { // 创建默认的httpClient实例. CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httppost HttpPost httppost = new HttpPost(url); // 创建参数队列 List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("account", name)); formparams.add(new BasicNameValuePair("passwd", pwd)); formparams.add(new BasicNameValuePair("phone", phone)); formparams.add(new BasicNameValuePair("content", content)); UrlEncodedFormEntity uefEntity; try { uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); httppost.setEntity(uefEntity); System.out.println("executing request " + httppost.getURI()); CloseableHttpResponse response = httpclient.execute(httppost); try { HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println("--------------------------------------"); System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8")); System.out.println("--------------------------------------"); } } finally { response.close(); } } catch (Exception e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } }}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1142
183.71 KB下载数642
644.84 KB下载数2755
相关文章
发现更多好内容猜你喜欢
AI推送时光机服务器的真实IP怎么利用java进行获取
服务器2023-05-31
利用Nginx 反向代理怎么获取真实IP
服务器2023-06-08
怎么使用java获取服务器ip
服务器2023-06-04
客户端的IP地址与MAC地址怎么利用Java进行获取
服务器2023-05-31
Python实现获取域名所用服务器的真实IP
服务器2022-06-04
字节码文件怎么利用java进行获取
服务器2023-05-31
怎么使用java获取服务器ip地址
服务器2023-06-07
怎么获取服务器的IP地址
服务器2023-07-06
Java中的子文本怎么利用正则表达式进行获取
服务器2023-05-31
WCF服务怎么利用JAVA进行调用
服务器2023-05-31
利用java怎么与web服务器进行链接
服务器2023-05-31
怎么获取云服务器的ip地址
服务器2023-08-15
Jar中的文件怎么利用Java进行读取
服务器2023-05-31
使用爬虫怎么获取代理服务器ip
服务器2023-06-14
如何从eureka获取服务的ip和端口号进行Http的调用
服务器2024-04-02
类路径下的文件怎么利用Java进行读取
服务器2023-05-31
利用Jacoco怎么获取Java 程序代码执行的覆盖率
服务器2023-06-06
怎么利用JAVA实现可以自行关闭服务器
服务器2023-06-20
Ubuntu中怎么利用TCP协议获取服务器时间
服务器2023-06-13
在Java项目中使用Request怎么实现获取客户端的IP地址
服务器2023-05-31
咦!没有更多了?去看看其它编程学习网 内容吧