Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
common
Project
Project
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
曲欣亮
common
Commits
c5b29911
Commit
c5b29911
authored
Aug 22, 2019
by
Quxl
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
http://key@gitlab.egolm.com/key/common.git
parents
9643dbbf
59d59db7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
72 deletions
+88
-72
ServletUtil.java
src/main/java/com/egolm/common/web/ServletUtil.java
+88
-72
No files found.
src/main/java/com/egolm/common/web/ServletUtil.java
View file @
c5b29911
package
com
.
egolm
.
common
.
web
;
import
java.net.InetAddress
;
import
java.net.URLDecoder
;
import
java.net.UnknownHostException
;
import
java.util.Enumeration
;
import
javax.servlet.http.HttpServletRequest
;
import
com.egolm.common.StringUtil
;
import
com.egolm.common.exception.PluginException
;
public
class
ServletUtil
{
public
static
String
remoteIp
(
HttpServletRequest
request
)
{
String
ip
=
request
.
getHeader
(
"X-Real_IP"
);
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
ip
=
request
.
getHeader
(
"X-Forwarded-For"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
ip
=
request
.
getHeader
(
"Proxy-Client-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
ip
=
request
.
getHeader
(
"WL-Proxy-Client-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
ip
=
request
.
getRemoteAddr
();
}
String
[]
ips
=
ip
.
trim
().
split
(
", "
);
return
ips
[
ips
.
length
-
1
].
trim
();
}
public
static
String
getLocalIP
()
{
try
{
return
InetAddress
.
getLocalHost
().
getHostAddress
();
}
catch
(
UnknownHostException
e
)
{
throw
new
PluginException
(
e
);
}
}
/**
*
* @Description 读取传入的json字符
* @param request
* @return
* @return String
* @throws
* @author 曲欣亮
* @date 2016年5月17日
* @since 2016年5月17日
*/
public
static
String
readReqJson
(
HttpServletRequest
request
)
{
String
json
=
""
;
try
{
Enumeration
<?>
names
=
request
.
getParameterNames
();
if
(
names
.
hasMoreElements
())
{
json
=
(
String
)
names
.
nextElement
();
}
if
(
json
==
null
||
json
.
length
()
==
0
)
{
json
=
StringUtil
.
read
(
request
.
getReader
());
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
try
{
String
encodeJson
=
URLDecoder
.
decode
(
json
,
"UTF-8"
);
return
encodeJson
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
json
;
}
}
}
package
com
.
egolm
.
common
.
web
;
import
java.io.UnsupportedEncodingException
;
import
java.net.InetAddress
;
import
java.net.URLDecoder
;
import
java.net.URLEncoder
;
import
java.net.UnknownHostException
;
import
java.util.Enumeration
;
import
javax.servlet.http.HttpServletRequest
;
import
com.egolm.common.StringUtil
;
import
com.egolm.common.exception.PluginException
;
public
class
ServletUtil
{
public
static
String
remoteIp
(
HttpServletRequest
request
)
{
String
ip
=
request
.
getHeader
(
"X-Real_IP"
);
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
ip
=
request
.
getHeader
(
"X-Forwarded-For"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
ip
=
request
.
getHeader
(
"Proxy-Client-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
ip
=
request
.
getHeader
(
"WL-Proxy-Client-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
ip
=
request
.
getRemoteAddr
();
}
String
[]
ips
=
ip
.
trim
().
split
(
", "
);
return
ips
[
ips
.
length
-
1
].
trim
();
}
public
static
String
getLocalIP
()
{
try
{
return
InetAddress
.
getLocalHost
().
getHostAddress
();
}
catch
(
UnknownHostException
e
)
{
throw
new
PluginException
(
e
);
}
}
/**
*
* @Description 读取传入的json字符
* @param request
* @return
* @return String
* @throws
* @author 曲欣亮
* @date 2016年5月17日
* @since 2016年5月17日
*/
public
static
String
readReqJson
(
HttpServletRequest
request
)
{
String
json
=
""
;
try
{
Enumeration
<?>
names
=
request
.
getParameterNames
();
if
(
names
.
hasMoreElements
())
{
json
=
(
String
)
names
.
nextElement
();
}
if
(
json
==
null
||
json
.
length
()
==
0
)
{
json
=
StringUtil
.
read
(
request
.
getReader
());
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
try
{
String
encodeJson
=
URLDecoder
.
decode
(
json
,
"UTF-8"
);
return
encodeJson
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
json
;
}
}
public
static
String
covertFileName
(
HttpServletRequest
request
,
String
fileName
)
throws
UnsupportedEncodingException
{
String
userAgent
=
request
.
getHeader
(
"User-Agent"
).
toLowerCase
();
System
.
out
.
println
(
userAgent
);
String
excelFileName
=
""
;
if
(
userAgent
.
indexOf
(
"msie"
)
!=
-
1
)
{
// IE浏览器
excelFileName
=
URLEncoder
.
encode
(
fileName
,
"UTF8"
);
}
else
if
(
userAgent
.
indexOf
(
"firefox"
)
!=
-
1
)
{
// google,火狐浏览器
excelFileName
=
new
String
(
fileName
.
getBytes
(),
"ISO-8859-1"
);
}
else
{
excelFileName
=
URLEncoder
.
encode
(
fileName
,
"UTF8"
);
// 其他浏览器
}
return
excelFileName
;
}
}
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