获取web访问路径的ip,项目的本地路路径,生成随机数
package com.evergreen.util;
import java.io.File;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
public class PathCtrl {
public PathCtrl() {
}
//得到访问项目的本地路路径
public String getSysPath(String strFolder) {
String strCode = System.getProperty("user.dir");
String strPath = strCode + File.separator + strFolder;
File fl = new File(strPath);
if (!fl.exists()) {
fl.mkdir();
}
return strPath;
}
//获取访问的IP地址
public String getIPAddress(HttpServletRequest request) {
String strReturn = request.getHeader("x-forwarded-for");
if (strReturn == null || strReturn.length() == 0 || "unknown".equalsIgnoreCase(strReturn)) {
strReturn = request.getHeader("Proxy-Client-IP");
}
if (strReturn == null || strReturn.length() == 0 || "unknown".equalsIgnoreCase(strReturn)) {
strReturn = request.getHeader("WL-Proxy-Client-IP");
}
if (strReturn == null || strReturn.length() == 0 || "unknown".equalsIgnoreCase(strReturn)) {
strReturn = request.getHeader("HTTP_Client_IP");
}
if (strReturn == null || strReturn.length() == 0 || "unknown".equalsIgnoreCase(strReturn)) {
strReturn = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (strReturn == null || strReturn.length() == 0 || "unknown".equalsIgnoreCase(strReturn)) {
strReturn = request.getRemoteAddr();
}
if (strReturn != null && strReturn.length() > 15 && strReturn.indexOf(",") > 0) {
strReturn = strReturn.substring(0, strReturn.indexOf(","));
}
return strReturn;
}
//生成随机树
public String getRandomString(int iLength) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for(int i = 0; i < iLength; ++i) {
int number = random.nextInt(str.length());
sb.append(str.charAt(number));
}
return sb.toString();
}
}