今天给大家做一个装的东西,我们有时候看到别人网站可以获取系统信息,然后自己也想要,但是自己又不会去写,怎么办呢,我现在教大家如何去写获取系统信息,就是如下图所示

大家可以看到,我在网站主页获取了一系列系统信息,这样前端也能看到系统数据

代码如下:

       Properties props = System.getProperties();// 获取当前的系统属性
        HashMap<String, Object> map = new HashMap<>();
        map.put("CPU核数", String.valueOf(Runtime.getRuntime().availableProcessors()));

        // 单位B 转换为M
        map.put("虚拟机内存总量", String.valueOf(Runtime.getRuntime().totalMemory() / 1048576));
        map.put("虚拟机空闲内存量", String.valueOf(Runtime.getRuntime().freeMemory() / 1048576));
        map.put("虚拟机使用最大内存量", String.valueOf(Runtime.getRuntime().maxMemory() / 1048576));

        map.put("系统名称", props.getProperty("os.name"));
        map.put("系统构架", props.getProperty("os.arch"));
        map.put("系统版本", props.getProperty("os.version"));

        map.put("Java版本", props.getProperty("java.version"));
        map.put("Java安装路径", props.getProperty("java.home"));

        CpuInfo cpu = OshiUtil.getCpuInfo();
        map.put("cpu信息", cpu.getCpuModel() + "" + cpu.getCpuNum());
        map.put("内存总量", OshiUtil.getMemory().getTotal() / 1048576);
        map.put("内存可用", OshiUtil.getMemory().getAvailable() / 1048576);

        //数据库
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            DatabaseMetaData mtdt = connection.getMetaData();
            map.put("数据库链接", mtdt.getURL());
            map.put("数据库",mtdt.getDatabaseProductName());
            map.put("数据库版本",mtdt.getDatabaseProductVersion());

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        ServletContext context = request.getServletContext();
        map.put("web容器",context.getServerInfo());
        map.put("发布路径",context.getRealPath(""));

大家不需要的可以自行删除,需要大家导一个糊涂的工具包,自行百度