java获得IP地址
public static String getIpAddress(HttpServletRequest request) {
// 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址
String ip = request.getHeader("X-Forwarded-For");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
} else if (ip.length() > 15) {
String[] ips = ip.split(",");
for (int index = 0; index < ips.length; index++) {
String strIp = (String) ips[index];
if (!("unknown".equalsIgnoreCase(strIp))) {
ip = strIp;
break;
}
}
}
return ip;
}
本地文件截取获得国家二字代码 -----> 附链接:文件下载地址
public static void main(String[] args) throws Exception {
Country("C:\\Users\\Administrator\\Desktop\\Country.txt");
}
private static void Country(String file) {
File file1 = new File(file);
Map<String, String> map2 = new HashMap();
BufferedReader br = null;
String str = null;
try {
br = new BufferedReader(new FileReader(file1));
while ((str = br.readLine()) != null) {
int i1 = str.lastIndexOf("-");
int i = str.lastIndexOf("(");
if (i1 > 1) {
String substring = str.substring(i1); // 得到“-”还后面的字符
String toCharacter = str.substring(0, str.length() - substring.length()); // 得到二字代码
String substring3 = str.substring(str.length() - str.substring(i + 1).length()); // 得到括号后的文字
String countryName = null;
if (str.lastIndexOf(")") == 0) { // 判断后面是否有“)”
countryName = str.substring(str.length() - substring3.length(), str.length() + 1);
} else {
countryName = str.substring(str.length() - substring3.length(), str.length() - 2);
}
map2.put(countryName, toCharacter);
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(map2);
}