1,如何擷取浏覽器的ip
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuIXY0N3Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmL3gjN5gzN3gjMxcHavw1LcpDc0RHaiojIsJye.png)
/***
* 擷取用戶端ip位址(可以穿透代理)
* @param request
* @return
*/
public static string getclientipaddr(httpservletrequest request) {
string ip = request.getheader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsignorecase(ip)) {
ip = request.getheader("proxy-client-ip");
}
ip = request.getheader("wl-proxy-client-ip");
ip = request.getheader("http_x_forwarded_for");
ip = request.getheader("http_x_forwarded");
ip = request.getheader("http_x_cluster_client_ip");
ip = request.getheader("http_client_ip");
ip = request.getheader("http_forwarded_for");
ip = request.getheader("http_forwarded");
ip = request.getheader("http_via");
ip = request.getheader("remote_addr");
ip = request.getremoteaddr();
return ip;
}
public static string getipaddr(httpservletrequest request) {
string ip = request.getheader("x-real-ip");
if (null != ip && !"".equals(ip.trim())
&& !"unknown".equalsignorecase(ip)) {
return ip;
}
ip = request.getheader("x-forwarded-for");
// get first ip from proxy ip
int index = ip.indexof(',');
if (index != -1) {
return ip.substring(0, index);
} else {
return ip;
}
return request.getremoteaddr();
2,如何判斷伺服器是否是本機
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuIXY0N3Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmL3gjN5gzN3gjMxcHavw1LcpDc0RHaiojIsJye.png)
* 伺服器是否是本機
public static boolean islocalip(httpservletrequest request){
string ip=webservletutil.getclientipaddr(request);
return ip.equals("127.0.0.1")||ip.equals("localhost")||ip.equals("0:0:0:0:0:0:0:1");
3,應用:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuIXY0N3Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmL3gjN5gzN3gjMxcHavw1LcpDc0RHaiojIsJye.png)
* favicon.ico
* @throws ioexception
@requestmapping(value = "/favicon.ico")
public responseentity<byte[]> faviconico(httpservletrequest request) throws ioexception {
httpheaders headers = new httpheaders();
headers.setcontenttype(mediatype.image_png);
string faviconiconame="sms-4.ico";
headers.set(constant2.content_disposition,webservletutil.getcontentdisposition(true, faviconiconame));
///home/whuang/software/apache-tomcat-7.0.53/webapps/root/
string webapppath=null;
if(webservletutil.islocalip(request)){//伺服器在本機(通路ip為127或localhost)
webapppath=webservletutil.getrealpath(request);
}else{
webapppath=dictionaryparam.get(constant2.dictionary_group_global_setting, "web-inf_loc");
return new responseentity<byte[]>(fileutils.getbytes4file(
webapppath
+"web-inf/static/img/"+faviconiconame),
headers, httpstatus.created);
4,如何擷取伺服器部署的本地路徑
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuIXY0N3Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmL3gjN5gzN3gjMxcHavw1LcpDc0RHaiojIsJye.png)
public static string getrealpath(httpservletrequest request) {
return getrealpath(request, "\\");
/**
* 擷取相對位址的絕對位址
*
* @param relativepath
public static string getrealpath(httpservletrequest request, string relativepath) {
return request.getsession().getservletcontext().getrealpath(relativepath);