本文目录一览:
- 1、java如何获取用户真实的ip
- 2、java如何获取公网ip,有通过路由
- 3、java IP查询方法
- 4、java怎么获取请求的ip
- 5、JAVA如何限制用户IP地址?
- 6、如何用 Java 获取系统 IP
java如何获取用户真实的ip
1、如果服务器如果没有采用反向代理,而且客户端没有用正向代理的话,那么可以获取客户端的真实IP地址request.getRemoteAddr()
2、如果服务器如果没有采用反向代理,而且客户端有用正向代理的话,那么通过request.getRemoteAddr()获取客户端的IP地址是客户端 的代理服务器的地址,并不是客户端的真实地址,
3、如果客户端使用的是多层代理的话,服务器获得的客户端地址是客户端的最外围代理服务器的地址如果服务器如果采用反向代理服务器,不管客户端采用的是何种方式访问服务器。
//获得客户端真实IP地址的方法一:
public String getRemortIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
//获得客户端真实IP地址的方法二:
public String getIpAddr(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.getRemoteAddr();
}
return ip;
}
java如何获取公网ip,有通过路由
如果要通过路由器,不同的路由器的获取方法不一样。通用的做法是通过 HttpClient 在百度上搜索关键字 ip, 然后提取出公网ip。
代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class App {
// 获取网页源码
static String httpGet(String url) {
StringBuffer buffer = new StringBuffer();
try {
URLConnection conn = new URL(url).openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36");
try (InputStream inputStream = conn.getInputStream();
InputStreamReader streamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(streamReader);) {
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line).append(System.lineSeparator());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return buffer.toString();
}
public static void main(String[] args) {
String html = httpGet("");
// 提出IP
Pattern pattern = Pattern.compile("span\\sclass=\"c-gap-right\"本机IP:nbsp;([^]+)/span");
Matcher matcher = pattern.matcher(html);
if (matcher.find()) {
String ip = matcher.group(1);
System.out.println(ip);
}
}
}
java IP查询方法
Java编程查询IP地址归属地,可以调用淘宝提供的service查询,并且解析http请求返回的json串,代码如下:
package getAddressByIp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import net.sf.json.JSONObject;
public class GetAddressByIp
{
/**
*
* @param IP
* @return
*/
public static String GetAddressByIp(String IP){
String resout = "";
try{
String str = getJsonContent(""+IP);
System.out.println(str);
JSONObject obj = JSONObject.fromObject(str);
JSONObject obj2 = (JSONObject) obj.get("data");
String code = (String) obj.get("code");
if(code.equals("0")){
resout = obj2.get("country")+"--" +obj2.get("area")+"--" +obj2.get("city")+"--" +obj2.get("isp");
}else{
resout = "IP地址有误";
}
}catch(Exception e){
e.printStackTrace();
resout = "获取IP地址异常:"+e.getMessage();
}
return resout;
}
public static String getJsonContent(String urlStr)
{
try
{// 获取HttpURLConnection连接对象
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
// 设置连接属性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
// 获取相应码
int respCode = httpConn.getResponseCode();
if (respCode == 200)
{
return ConvertStream2Json(httpConn.getInputStream());
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return "";
}
private static String ConvertStream2Json(InputStream inputStream)
{
String jsonStr = "";
// ByteArrayOutputStream相当于内存输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 将输入流转移到内存输出流中
try
{
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)
{
out.write(buffer, 0, len);
}
// 将内存流转换为字符串
jsonStr = new String(out.toByteArray());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
}
java怎么获取请求的ip
java获取外网ip地址方法:
public class Main {
public static void main(String[] args) throws SocketException {
System.out.println(Main.getRealIp());
}
public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果没有配置外网IP则返回它
String netip = null;// 外网IP
EnumerationNetworkInterface netInterfaces =
NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaces.hasMoreElements() !finded) {
NetworkInterface ni = netInterfaces.nextElement();
EnumerationInetAddress address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress()
!ip.isLoopbackAddress()
ip.getHostAddress().indexOf(":") == -1) {// 外网IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress()
!ip.isLoopbackAddress()
ip.getHostAddress().indexOf(":") == -1) {// 内网IP
localip = ip.getHostAddress();
}
}
}
if (netip != null !"".equals(netip)) {
return netip;
} else {
return localip;
}
}
}
JAVA如何限制用户IP地址?
java web中限制访问的ip,主要是使用session的getRemortIP(HttpServletRequest)方法,如下代码:
/**
*
* @param request
* @return
*
* 功 能 :得到用户Ip地址
*/
public static String getRemortIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
调用
String ip = getRemortIP(HttpServletRequest);
if(ip == "127.0.0.1"){
System.out.print("禁止访问");
}
Hr完整项目中 --- common.jsp 得到网址
%
//路径
String path = request.getContextPath();
// request 保存
request.setAttribute("path",path);
%
如何用 Java 获取系统 IP
import java.net.*;
public class Test6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InetAddress ia=null;
try {
ia=ia.getLocalHost();
String localname=ia.getHostName();
String localip=ia.getHostAddress();
System.out.println("本机名称是:"+ localname);
System.out.println("本机的ip是 :"+localip);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}