在java中执行linux命令并将输出显示到html表 [英] Execute linux command in java and display output to html table

查看:197
本文介绍了在java中执行linux命令并将输出显示到html表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上显示磁盘信息的jsp代码 df -h
如何用表格显示html的输出?
代码下方:

I have jsp code df -h for display disk information on the website. How can I show the output to html with table? Below the code:

String[] disk;
String line;
String process;
Process p;
BufferedReader input;
    p = Runtime.getRuntime().exec("df -h");
    input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    input.readLine();
    disk = input.readLine().split("\\s+");
<%      
<tr bgcolor="#f0f0f0">
        <td colspan="2">
            <b>Disk</b>
        </td>
        <td align="center">
            <b>Size<b>
        </td>
        <td align="center">
            <b>Used<b>
        </td>
        <td align="center">
            <b>Avail<b>
        </td>
        <td align="center">
            <b>Use%<b>
        </td>
        <td align="center">
            <b>Mount<b>
        </td>
    </tr>
    while ((line = input.readLine()) != null) {
%>
        <tr>
        <td colspan="2"><% out.println(disk[0]); %></td>
        <td align="center"><% out.println(disk[1]); %></td>
        <td align="center"><% out.println(disk[2]); %></td>
        <td align="center"><% out.println(disk[3]); %></td>
        <td align="center"><% out.println(disk[4]); %></td>
        <td align="center"><% out.println(disk[5]); %></td>
        </tr>
<%
        }
    input.close(); 
%>

在linux上执行 df -h 时显示如下:

when on linux I execute "df -h" it showing like below:

[root@svr1 apache-tomcat-7.0.32]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/centos_svr1-root   29G  5.3G   23G  19% /
devtmpfs                      1.9G     0  1.9G   0% /dev
tmpfs                         1.9G     0  1.9G   0% /dev/shm
tmpfs                         1.9G   17M  1.9G   1% /run
tmpfs                         1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/vda1                     497M  163M  334M  33% /boot
tmpfs                         380M     0  380M   0% /run/user/0
[root@svr1 apache-tomcat-7.0.32]#

但在html我得到如下:

But at html I got like below:

我如何修复它,或者需要更改哪些代码?

How I can fix it, or what code that I need to change?

推荐答案

您需要在中移动此行,而循环:

disk = input.readLine().split("\\s+");

这篇关于在java中执行linux命令并将输出显示到html表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆