【问题标题】:Execute linux command in java and display output to html table在java中执行linux命令并显示输出到html表
【发布时间】:2017-01-30 14:57:05
【问题描述】:

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

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”时,如下所示:

[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 中我得到如下:

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

【问题讨论】:

  • 考虑行 `disk = input.readLine().split("\\s+");` 将在您的代码中执行多少次。
  • @yole 应该是一次,当脚本执行时它将显示来自 df -h 的所有磁盘信息
  • @IHarrisMarfel 没有。再想一想。
  • readLine().split() 读取一行文本并将其拆分。 df 输出多少行?
  • @yole 在我的情况下它显示 7 行,但在每台机器上都会有所不同。

标签: java output html-table display linux-disk-free


【解决方案1】:

您需要将此行移到while 循环内:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2011-06-11
    • 2019-05-20
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多