Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Z
zhj-report
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
data-spider
zhj-report
Commits
0e308a9b
Commit
0e308a9b
authored
Nov 01, 2019
by
董建华
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加获取linux的IP的方法
parent
a64fcc6e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
154 additions
and
23 deletions
+154
-23
TestController.java
src/main/java/com/controller/TestController.java
+21
-19
ReportService.java
src/main/java/com/service/report/ReportService.java
+1
-1
IReportServiceImpl.java
...main/java/com/service/report/impl/IReportServiceImpl.java
+2
-3
IpUtil.java
src/main/java/com/util/IpUtil.java
+130
-0
No files found.
src/main/java/com/controller/TestController.java
View file @
0e308a9b
...
...
@@ -2,6 +2,7 @@ package com.controller;
import
com.alibaba.fastjson.JSON
;
import
com.google.common.collect.ImmutableMap
;
import
com.util.IpUtil
;
import
jdk.nashorn.internal.ir.ReturnNode
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.HttpStatus
;
...
...
@@ -12,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
import
java.net.InetAddress
;
import
java.net.UnknownHostException
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.UUID
;
import
java.util.concurrent.ConcurrentHashMap
;
...
...
@@ -27,54 +29,54 @@ import java.util.concurrent.ConcurrentHashMap;
@RestController
@RequestMapping
(
"/"
)
public
class
TestController
{
private
static
Map
<
String
,
String
>
map
=
new
ConcurrentHashMap
<>();
private
static
Map
<
String
,
String
>
map
=
new
ConcurrentHashMap
<>();
@RequestMapping
(
"/getAllIp"
)
public
List
<
String
>
getAllIp
()
{
return
IpUtil
.
getAllIP
();
}
@RequestMapping
(
"/getIp"
)
public
String
getIp
(){
try
{
String
ip
=
InetAddress
.
getLocalHost
().
getHostAddress
();
return
ip
;
}
catch
(
UnknownHostException
e
)
{
e
.
printStackTrace
();
}
return
null
;
public
String
getIp
()
{
return
IpUtil
.
getLocalIP
();
}
@RequestMapping
(
value
=
"check"
)
public
ResponseEntity
<
String
>
check
(){
if
(
map
!=
null
&&
map
.
size
()>
0
)
{
@RequestMapping
(
value
=
"check"
)
public
ResponseEntity
<
String
>
check
()
{
if
(
map
!=
null
&&
map
.
size
()
>
0
)
{
return
new
ResponseEntity
<
String
>(
HttpStatus
.
NOT_FOUND
);
}
else
{
}
else
{
return
new
ResponseEntity
<
String
>(
HttpStatus
.
OK
);
}
}
@RequestMapping
(
value
=
"/online"
)
public
Map
<
String
,
String
>
online
(){
@RequestMapping
(
value
=
"/online"
)
public
Map
<
String
,
String
>
online
()
{
log
.
info
(
"应用上线,流量开始切入...."
);
map
.
clear
();
return
responseOk
();
}
@RequestMapping
(
value
=
"/offline"
)
public
Map
<
String
,
String
>
offline
(){
@RequestMapping
(
value
=
"/offline"
)
public
Map
<
String
,
String
>
offline
()
{
log
.
info
(
"应用下线,流量不再切入...."
);
map
.
put
(
"status"
,
"offline"
);
return
responseOk
();
}
@RequestMapping
(
value
=
"/test"
)
@RequestMapping
(
value
=
"/test"
)
public
Map
<
String
,
String
>
test
()
{
log
.
info
(
"CheckController测试!map = {}"
,
map
);
return
ImmutableMap
.
of
(
"code"
,
"0"
,
"msg"
,
JSON
.
toJSONString
(
map
));
}
public
static
Map
<
String
,
String
>
responseOk
()
{
return
ImmutableMap
.
of
(
"code"
,
"0"
,
"msg"
,
"ok"
);
}
@RequestMapping
(
"/getuuid"
)
public
String
getUuid
(
String
param
){
public
String
getUuid
(
String
param
)
{
return
UUID
.
nameUUIDFromBytes
(
param
.
getBytes
()).
toString
();
}
...
...
src/main/java/com/service/ReportService.java
→
src/main/java/com/service/
report/
ReportService.java
View file @
0e308a9b
package
com
.
service
;
package
com
.
service
.
report
;
import
com.emums.BusinessType
;
import
com.emums.InfoType
;
...
...
src/main/java/com/service/impl/IReportServiceImpl.java
→
src/main/java/com/service/
report/
impl/IReportServiceImpl.java
View file @
0e308a9b
package
com
.
service
.
impl
;
package
com
.
service
.
report
.
impl
;
import
com.config.ReportConfig
;
import
com.emums.BusinessType
;
...
...
@@ -7,7 +7,7 @@ import com.emums.PathType;
import
com.entity.FileEntry
;
import
com.entity.report.ReportRecord
;
import
com.mapper.report.ReportRecordMapper
;
import
com.service.ReportService
;
import
com.service.
report.
ReportService
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.util.DateUtil
;
import
com.util.DingTalk
;
...
...
@@ -22,7 +22,6 @@ import org.springframework.beans.factory.annotation.Qualifier;
import
org.springframework.stereotype.Service
;
import
java.io.*
;
import
java.nio.file.Path
;
import
java.util.*
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipOutputStream
;
...
...
src/main/java/com/util/IpUtil.java
0 → 100644
View file @
0e308a9b
package
com
.
util
;
import
java.net.InetAddress
;
import
java.net.NetworkInterface
;
import
java.net.UnknownHostException
;
import
java.util.ArrayList
;
import
java.util.Enumeration
;
import
java.util.List
;
/**
* @author :dongjianhua
* @date :Created in 2019/11/1 15:42
* @description:获取本机IP的工具类
* @modified By:
* @version: 1.0
*/
public
class
IpUtil
{
/**
* 02
* 获得主机IP
* 03
* <p>
* 04
*
* @return String
* 05
*/
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
List
<
String
>
getAllIP
()
{
List
<
String
>
list
=
new
ArrayList
<>();
try
{
if
(
isWindowsOS
())
{
InetAddress
ip
=
InetAddress
.
getLocalHost
();
list
.
add
(
ip
.
getHostAddress
());
return
list
;
}
InetAddress
ipAdd
=
null
;
Enumeration
<
NetworkInterface
>
netInterfaces
=
(
Enumeration
<
NetworkInterface
>)
NetworkInterface
.
getNetworkInterfaces
();
while
(
netInterfaces
.
hasMoreElements
())
{
NetworkInterface
ni
=
(
NetworkInterface
)
netInterfaces
.
nextElement
();
//----------特定情况,可以考虑用ni.getName判断
//遍历所有ip
Enumeration
<
InetAddress
>
ips
=
ni
.
getInetAddresses
();
while
(
ips
.
hasMoreElements
())
{
ipAdd
=
(
InetAddress
)
ips
.
nextElement
();
list
.
add
(
ipAdd
.
getHostAddress
());
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
list
;
}
/**
* 16
* 获取本机ip地址,并自动区分Windows还是linux操作系统
* 17
*
* @return String
* 18
*/
public
static
String
getLocalIP
()
{
String
sIP
=
""
;
InetAddress
ip
=
null
;
try
{
//如果是Windows操作系统
if
(
isWindowsOS
())
{
ip
=
InetAddress
.
getLocalHost
();
}
else
{
//如果是Linux操作系统
boolean
bFindIP
=
false
;
Enumeration
<
NetworkInterface
>
netInterfaces
=
(
Enumeration
<
NetworkInterface
>)
NetworkInterface
.
getNetworkInterfaces
();
while
(
netInterfaces
.
hasMoreElements
())
{
if
(
bFindIP
)
{
break
;
}
NetworkInterface
ni
=
(
NetworkInterface
)
netInterfaces
.
nextElement
();
//----------特定情况,可以考虑用ni.getName判断
//遍历所有ip
Enumeration
<
InetAddress
>
ips
=
ni
.
getInetAddresses
();
while
(
ips
.
hasMoreElements
())
{
ip
=
(
InetAddress
)
ips
.
nextElement
();
if
(
ip
.
isSiteLocalAddress
()
&&
!
ip
.
isLoopbackAddress
()
//127.开头的都是lookback地址
&&
ip
.
getHostAddress
().
indexOf
(
":"
)
==
-
1
)
{
bFindIP
=
true
;
break
;
}
}
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
if
(
null
!=
ip
)
{
sIP
=
ip
.
getHostAddress
();
}
return
sIP
;
}
}
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