Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
midjourney-proxy
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
徐少华
midjourney-proxy
Commits
e2c40352
Commit
e2c40352
authored
Feb 27, 2019
by
songxiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
获取本机IP地址
parent
810b0b38
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
2 deletions
+52
-2
NginxUtils.java
...c/main/java/com/pcloud/common/utils/nginx/NginxUtils.java
+52
-2
No files found.
pcloud-common/src/main/java/com/pcloud/common/utils/nginx/NginxUtils.java
View file @
e2c40352
...
...
@@ -3,6 +3,11 @@
*/
package
com
.
pcloud
.
common
.
utils
.
nginx
;
import
java.net.InetAddress
;
import
java.net.NetworkInterface
;
import
java.net.UnknownHostException
;
import
java.util.Enumeration
;
import
javax.servlet.http.HttpServletRequest
;
import
org.apache.commons.lang.StringUtils
;
...
...
@@ -10,8 +15,7 @@ import org.apache.commons.lang.StringUtils;
/**
* @描述:Nginx工具类
* @作者:songx
* @创建时间:2016年12月3日,下午6:06:07
* @版本:1.0
* @创建时间:2016年12月3日,下午6:06:07 @版本:1.0
*/
public
class
NginxUtils
{
...
...
@@ -20,6 +24,7 @@ public class NginxUtils {
* 址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的。但是在通过了Apache,Squid等
* 反向代理软件就不能获取到客户端的真实IP地址了。
* 但是在转发请求的HTTP头信息中,增加了X-FORWARDED-FOR信息。用以跟踪原有的客户端IP地址和原来客户端请求的服务器地址。
*
* @param request
* @return
*/
...
...
@@ -43,6 +48,7 @@ public class NginxUtils {
/**
* 获得浏览器请求头中的User-Agent
*
* @param request
* @return
*/
...
...
@@ -51,4 +57,48 @@ public class NginxUtils {
return
userAgent
;
}
/**
* 获取本机IP
*
* @return
* @throws UnknownHostException
*/
public
static
String
getLocalAddress
()
{
try
{
InetAddress
candidateAddress
=
null
;
// 遍历所有的网络接口
for
(
Enumeration
<
NetworkInterface
>
ifaces
=
NetworkInterface
.
getNetworkInterfaces
();
ifaces
.
hasMoreElements
();)
{
NetworkInterface
iface
=
(
NetworkInterface
)
ifaces
.
nextElement
();
// 在所有的接口下再遍历IP
for
(
Enumeration
<
InetAddress
>
inetAddrs
=
iface
.
getInetAddresses
();
inetAddrs
.
hasMoreElements
();)
{
InetAddress
inetAddr
=
(
InetAddress
)
inetAddrs
.
nextElement
();
if
(!
inetAddr
.
isLoopbackAddress
())
{
// 排除loopback类型地址
if
(
inetAddr
.
isSiteLocalAddress
())
{
// 如果是site-local地址,就是它了
return
inetAddr
.
getHostAddress
();
}
else
if
(
candidateAddress
==
null
)
{
// site-local类型的地址未被发现,先记录候选地址
candidateAddress
=
inetAddr
;
}
}
}
}
if
(
candidateAddress
!=
null
)
{
return
candidateAddress
.
getHostAddress
();
}
// 如果没有发现 non-loopback地址.只能用最次选的方案
InetAddress
jdkSuppliedAddress
=
InetAddress
.
getLocalHost
();
if
(
jdkSuppliedAddress
==
null
)
{
throw
new
UnknownHostException
(
"The JDK InetAddress.getLocalHost() method unexpectedly returned null."
);
}
return
jdkSuppliedAddress
.
getHostAddress
();
}
catch
(
Exception
e
)
{
UnknownHostException
unknownHostException
=
new
UnknownHostException
(
"Failed to determine LAN address: "
+
e
);
unknownHostException
.
initCause
(
e
);
return
null
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment