在JTextPane中显示Midnight Commander屏幕 [英] Displaying Midnight Commander screen in JTextPane

查看:57
本文介绍了在JTextPane中显示Midnight Commander屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过SSH连接到服务器并获取命令的输出.如果放入System.out,一切正常.否则,如果我想将其放入JTextPane,则将其放入,但是MC不可读.

I've tried to connect by SSH to a server and get an output for my commands. Everything works fine if I put out into System.out. Else if I want to put it into JTextPane, it puts it, but MC is unreadable.

这是我的代码:

JSch jsch = new JSch();

String command = "";
String commandR = "";
host = null;

if (arg.length > 1) {
    host = arg[0];
    command = arg[2];
    commandR = arg[3];
}

String user = host.substring(0, host.indexOf('@'));
host = host.substring(host.indexOf('@') + 1);
Session session = jsch.getSession(user, host, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);

session.setPassword(arg[1]);
if (!session.isConnected()) {
    session.connect();
}

Channel channel = session.openChannel("shell");

String ans = " ";
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
//channel.setInputStream(System.in);
if (!channel.isConnected()) {
    channel.connect(10 * 1000);
}
boolean logon = false;
boolean started = false;
boolean brtuser = false;
boolean log = false;
byte[] tmp = new byte[1024];

JFrame j = new JFrame();
JTextPane jj = new JTextPane();
jj.setContentType("text/html");
StyledDocument  doc = (StyledDocument )jj.getDocument();
jj.setPreferredSize(new Dimension(500, 600));
j.setDefaultCloseOperation(2);
j.add(new JScrollPane(jj));
j.pack();
j.setVisible(true);
while (!started) {

    while (in.available() > 0) {
        int i = in.read(tmp, 0, 1024);
        if (i < 0) {
            break;
        }

        // jj.append();

        doc.insertString(doc.getLength(), new String(tmp, 0, i), null);
        ans += new String(tmp, 0, i);
        System.out.print(new String(tmp, 0, i));

然后我写pwdmc-pwd我很好,但是mc

Then I write pwd and mc - pwd I get fine, but mc is such as

39m[49m                                                                              
    [37m[40m                                                                                [23;3H[22;1H[39m[49mGNU Midnight Commander 4.6.0                                                    
      [1;1H[30m[46m  Left     File     Command     Options     Right                               
    [37m[44m+[0m[37m[44m<[37m[44m-[0m[37m[44m~[37m[44m---------------------------------[0m[37m[44mv>[37m[44m++[0m[30m[47m<[37m[44m-[0m[30m[47m~[37m[44m---------------------------------[0m[30m[47mv>[37m[44m+
    |[0m[1m[33m[44m       Name      [0m[37m[44m|[0m[1m[33m[44m Size  [0m[37m[44m|[0m[1m[33m[44m   MTime    [0m[37m[44m||[0m[1m[33m[44m       Name      [0m[37m[44m|[0m[1m[33m[44m Size  [0m[37m[44m|[0m[1m[33m[44m   MTime    [0m[37m[44m|
    |[0m[1m[37m[44m/..              [0m[37m[44m|[0m[1m[37m[44mUP--DIR[0m[37m[44m|[0m[1m[37m[44m            [0m[37m[44m||[0m[30m[46m/..              [30m[46m|[0m[30m[46mUP--DIR[30m[46m|[0m[30m[46m            [37m[44m|
    |[0m[1m[37m[44m/.mc             [0m[37m[44m|[0m[1m[37m[44m   1024[0m[37m[44m|[0m[1m[37m[44mJan 29 11:35[0m[37m[44m||[0m[1m[37m[44m/.mc    

请帮助我使其可见!

推荐答案

JTextPane中看到的垃圾"是 ANSI转义码.这些定义颜色和其他格式. Midnight Commander(和其他应用程序)使用它们在文本控制台上呈现类似于GUI的界面.

The "garbage" that you see in the JTextPane are ANSI escape codes. These define colors and other formatting. They used by Midnight Commander (and other applications) to render a GUI-like interface on a text console.

像PuTTY这样的终端/SSH客户端可以理解ANSI转义码.即使是各种操作系统中的控制台窗口(例如Windows).这可能就是为什么在将输出打印到System.out时看起来不错的原因.

The ANSI escape codes are understood by Terminal/SSH clients like PuTTY. Even by console windows in various OSes (e.g. Windows). That's probably, why it looks good when you print the output to System.out.

但是JTextPane无法理解它们,它会按原样显示它们.

But they are not understood by JTextPane, it displays them as they are.

有关"ANSI彩色JTextPane子类",请参见 Java Swing文本字段中的ANSI颜色.

See ANSI colors in Java Swing text fields for "ANSI colored JTextPane subclass".

这篇关于在JTextPane中显示Midnight Commander屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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