代码如下
package org.metarnet.bmcif.telnet;import java.io.InputStream;
import java.io.PrintStream;import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.telnet.TerminalTypeOptionHandler;
import org.apache.commons.net.telnet.*;
import org.apache.log4j.Logger;public class TelnetHandler {private static final Logger logger = Logger.getLogger(TelnetHandler.class);private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String prompt ;
String s;public void TelnetCmd( String server, String user, String password,String cmd){
try {
        // Connect to the specified server
telnet.connect( server, 23 );        logger.info("Login............................");
// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream( telnet.getOutputStream());//Login
readUntil( "login: " );
            write( user );
            readUntil( "assword: " );
            write( password );
           
            // Advance to a prompt
            prompt = ">";
            readUntil( prompt );            //Exec Cmd
            sendCmd(cmd);
           
}catch( Exception e ) {
e.printStackTrace();
logger.info("logon failed",e);
}
}public String readUntil( String pattern ) {
        try {
        char lastChar = pattern.charAt( pattern.length() - 1 );
        StringBuffer sb = new StringBuffer();
        //boolean found = false;
        char ch = ( char )in.read();
       
        while( true ) {
        System.setProperty("GBK","iso8859-1"); 
        System.out.print( ch );
        sb.append( ch );
        if( ch == lastChar ) {
        if( sb.toString().endsWith( pattern ) ) {
        //System.out.print( sb.toString());
        return sb.toString();
        }
        }
        ch = ( char )in.read();
        }                 
        }catch( Exception e ) {
        e.printStackTrace();
        }
        return null;
}public void write( String value ) {
try {
out.println( value );
out.flush();
System.out.println( value );
}
catch( Exception e ) {
e.printStackTrace();
}
}public String  sendCmd( String command ) {
try {
prompt = ">";
write( command );
return readUntil( s+prompt  );
}catch( Exception e ) {
e.printStackTrace() ;
}
return null;
}public void disconnect() {
try {
telnet.disconnect();
}catch( Exception e ) {
e.printStackTrace();
}
}public static void main( String[] args ) {
try {logger.info("发送命令开始");
TelnetHandler telnet = new TelnetHandler();
telnet.TelnetCmd("*****", "*****", "*****","dir");     
logger.info("发送命令结束");
telnet.disconnect();
}catch( Exception e ) {
e.printStackTrace();
}
}} 输出乱码如下* 

解决方案 »

  1.   

    把输入输出流都转为UTF-8 的所有编码都转为UTF-8 看看
      

  2.   

    感觉应该是编码不同 
    new InputStreamReader(s.getInputStream())); //出现部分乱码 InputStreamReader这个流中可以获取到输入的编码格式 getEncoding() 打出来看一下。乱码是因为输入数据的编码和你转换成字符串用的编码格式不一致。 
      

  3.   

    new TelnetClient("VT220");或许可以。
    摘自http://group.gimoo.net/review/159091
    【niunang回复于22日04点07分 
    最开始new TelnetClient()的时候没有设置参数,如下所示:
    private TelnetClient telnet = new TelnetClient();
    远程登录AIX、linux、sun 服务器执行脚本返回信息都正常,但远程登录windows执行脚本返回信息是乱码。后来在new TelnetClient()的时候设置了参数,如下所:
    private TelnetClient telnet = new TelnetClient("VT220");
    登录Windows 2003 和XP执行脚本返回信息就正常了。但是Windows 2000还是不行!
    是不是TelnetClient不支持2000呀,麻烦大哥们分析分析! 】
      

  4.   

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PrintStream;
    import java.io.UnsupportedEncodingException;import org.apache.commons.net.telnet.TelnetClient;public class TestSocket {
    private TelnetClient tc=new TelnetClient("VT220");
    private InputStream in=null;
    private PrintStream out=null;
    public TestSocket(){
    try {
    tc.connect("192.168.1.5",23);
    in=tc.getInputStream();
    out=new PrintStream(tc.getOutputStream());
    System.out.print(readUtil("login: "));
    write("wangchong");

    System.out.print(readUtil("Password: "));
    write("wangchong");

    System.out.print(readUtil("$ "));
    } catch (Exception e) {
    e.printStackTrace();

    }
    public String readUtil(String str){
    try {
    StringBuffer sb=new StringBuffer();
    while(true){
    char c=(char)in.read();
    sb.append(c);
    if(c==str.charAt(str.length()-1)&&sb.toString().endsWith(str)){
    return sb.toString();
    }
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return null;
    }
    public void write(String value){
    out.println(value);
    out.flush();
    System.out.println(value);
    }
    public String executeCommond(String commond){
    write(commond);
    return readUtil("$ ");
    }
    public void close(){
    try {
    tc.disconnect();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args) throws UnsupportedEncodingException {
    TestSocket ts=new TestSocket();
    String s=new String(ts.executeCommond("ls").getBytes("iso-8859-1"),"gbk");
    System.out.print(s);
    System.out.print(ts.executeCommond("ping 127.0.0.1 -c 2"));
    ts.close();
    }
    }