java - 如何从客户端程序中获取操作系统名称?
问题描述
程序的某些部分受操作系统类型的影响。所以我尝试按操作系统名称进行分支,如下所示。当我在本地运行它时,它可以工作。但是我将它上传到服务器后作为客户端程序运行,似乎无法获取操作系统名称。请让我知道如何从客户端程序中获取操作系统名称。谢谢。
public void getOSName() {
String osName = System.getProperty("os.name")
if(!osName.trim().toUpperCase().equals("WINDOWS 10")){
run();
}else{
}
}
解决方案
在 util 类下方结帐以进行操作系统验证。
使用系统属性:由于这个问题,这种方法可能会失败。请参阅Java 的 Windows 10 的“os.name”?
/**
* The Class OSValidator.
*/
public final class OSValidator {
/** The Constant OS. */
public static final String OS = System.getProperty("os.name").toLowerCase();
/**
* Checks if is windows 7.
*
* @return true, if is windows 7
*/
public static final boolean isWindows7() {
return (OS.indexOf("windows 7") >= 0);
}
/**
* Checks if is windows 10.
*
* @return true, if is windows 10
*/
public static final boolean isWindows10() {
return (OS.indexOf("windows 10") >= 0);
}
/**
* Checks if is mac.
*
* @return true, if is mac
*/
public static final boolean isMac() {
return (OS.indexOf("mac") >= 0);
}
}
使用 SystemUtils – Apache Commons Lang
public String getOperatingSystemSystemUtils() {
String os = SystemUtils.OS_NAME;
// System.out.println("Using SystemUtils: " + os);
return os;
}
推荐阅读
- javascript - 计算 Javascript 中所有字符串字符在字符串中使用字符的次数
- java - How can subclasses be identified when they are send over http into Java/Spring?
- here-api - 有没有办法检索 HERE RouteLink 的形状?
- html - 即使 url 是新的,浏览器也不会下载更新的 css、js 等
- python - 类型作为方法参数?
- crystal-reports - Raspberry Pi 3B+ 晶体振荡器 - 它有多准确?
- python-3.x - InvalidArgumentError:输入必须是向量,得到形状:[]
- google-play-console - Google Pay 生产环境
- javascript - 如何将对象数组中的元素从 api 推送到新数组 vue.js
- javascript - 更改身体不透明度关键帧