您的位置:首页 > 其它

前台获取信息

2016-02-26 12:25 246 查看
![这样的](https://img-blog.csdn.net/20160226111027254)


javascript代码
function detectOS() {
var sUserAgent = navigator.userAgent;
var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel");
if (isMac) return "Mac";
var isUnix = (navigator.platform == "X11") && !isWin && !isMac;
if (isUnix) return "Unix";
var isLinux = (String(navigator.platform).indexOf("Linux") > -1);
if (isLinux) return "Linux";
if (isWin) {
var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1;
if (isWin2K) return "Windows 2000";
var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1;
if (isWinXP) return "Windows XP";
var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1;
if (isWin2003) return "Windows 2003";
var isWinVista= sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1;
if (isWinVista) return "Windows Vista";
var isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1;
if (isWin7) return "Windows 7";
}
return "other";
}

function userAgent(){
var ua = navigator.userAgent;
ua = ua.toLowerCase();
var match = /(webkit)[ \/]([\w.]+)/.exec(ua) ||
/(opera)(?:.*version)?[ \/]([\w.]+)/.exec(ua) ||
/(msie) ([\w.]+)/.exec(ua) ||
!/compatible/.test(ua) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec(ua) || [];
//如果需要获取浏览器版本号:match[2]
switch(match[1]){
case "msie":      //ie
return "Internet Explorer "+parseInt(match[2]);
break;
case "webkit":     //safari or chrome
return "safari or chrome"+parseInt(match[2]);
break;
case "opera":      //opera
return "opera"+parseInt(match[2]);
break;
case "mozilla":    //Firefox
return "mozilla Firefox"+parseInt(match[2]);
break;
default:
break;
}
}
function openWords(conId){
window.location.href="${pageContext.request.contextPath}/content/edit.htm?contentId="+conId;
}


后台获取ip代码
/**
* 获取访问者IP
* 在一般情况下使用Request.getRemoteAddr()即可,但是经过nginx等反向代理软件后,这个方法会失效。
* 本方法先从Header中获取X-Real-IP,如果不存在再从X-Forwarded-For获得第一个IP(用,分割),
* 如果还不存在则调用Request .getRemoteAddr()。
*
* @param request
* @return
*/
protected String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("X-Real-IP");
if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
return ip;
}
ip = request.getHeader("X-Forwarded-For");
if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个IP值,第一个为真实IP。
int index = ip.indexOf(',');
if (index != -1) {
return ip.substring(0, index);
} else {
return ip;
}
} else {
return request.getRemoteAddr();
}
}


内存获取
public class OsInfo {

public String getTotalMemory() {
DecimalFormat dcmFmt = new DecimalFormat("0.0");
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
totalvirtualMemory = osmxb.getTotalPhysicalMemorySize();
float totals = (float) totalvirtualMemory / (1024 * 1024);
return dcmFmt.format(totals);
}

public String getFreeMemory() {
DecimalFormat dcmFmt = new DecimalFormat("0.0");
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
float frees = (float) freePhysicalMemorySize / (1024 * 1024);
return dcmFmt.format(frees);
}

public String getUsedMemory() {
DecimalFormat dcmFmt = new DecimalFormat("0.0");
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
totalvirtualMemory = osmxb.getTotalPhysicalMemorySize();
float totals = (float) totalvirtualMemory / (1024 * 1024);
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
float frees = (float) freePhysicalMemorySize / (1024 * 1024);
float compare = (float) (totals - frees);
return dcmFmt.format(compare);
}

}


HashMap<String, String> osInfo = new HashMap<String, String>();
osInfo.put("osName", (String) System.getProperty("os.name"));
osInfo.put("osArch", System.getProperty("os.arch"));
osInfo.put("osVersion", System.getProperty("os.version"));
osInfo.put("jvmName", System.getProperty("java.vm.name"));
osInfo.put("jvmVersion", System.getProperty("java.vm.version"));
osInfo.put("javaVersion", System.getProperty("java.version"));
osInfo.put("serverInfo", context.getServerInfo());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: