我尝试通过SSH连接到服务器并为我的命令获取输出。如果我进入System.out
,一切正常。否则,如果我想将它放入JTextPane
,它会说明,但MC是不可读的。
这是我的代码:
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));
然后我写pwd
和mc
- pwd
我很好,但mc
就是
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
请帮我看看它!
答案 0 :(得分:0)
&#34;垃圾&#34;您在JTextPane
中看到的是ANSI escape codes。这些定义了颜色和其他格式。 Midnight Commander(和其他应用程序)使用它们在文本控制台上呈现类似GUI的界面。
终端/ SSH客户端(如PuTTY)可以理解ANSI转义码。即使是各种操作系统(例如Windows)中的控制台窗口。这可能就是为什么当你将输出打印到System.out
时看起来很好。
但JTextPane
无法理解它们,它会按原样显示它们。
请参阅ANSI colors in Java Swing text fields了解&#34; ANSI着色JTextPane
子类&#34;。