SSH Apache Mina 读取和打印文件内容

SSH Apache Mina read and print file content

你好,我正在为服务器使用 apache mina,我想制作 echoshellfactory,其中 return 一些命令。

我做到了,但是当我想要命令打印 fr() 方法时,它会在 eclipse 控制台中打印,但不会在 Putty 控制台中打印。

我知道这是因为响应是字符串,但有什么方法可以使响应成为整个方法。

public class EchoShellFactory implements Factory<Command> {

public Command create() {
    return new EchoShell();
}

public static class EchoShell implements Command, Runnable {

    private static final String SHELL_THREAD_NAME = "name";
    private static final String SHELL_PROMPT = "aaa# ====> ";
    private static final String SHELL_CMD_QUIT = "quit";
    private static final String SHELL_CMD_EXIT = "exit";
    private static final String SHELL_CMD_VERSION = "version";
    private static final String SHELL_CMD_HELP = "help";
    private static final String SHELL_CMD_FILEREADER = "fr";
    private static final String SHELL_CMD_ENTER = "";

    private InputStream in;
    private OutputStream out;
    private OutputStream err;
    private ExitCallback callback;
    private Environment environment;
    private Thread thread;
    private ConsoleReader reader;
    private PrintWriter writer;

    public InputStream getIn() {
        return in;
    }

    public OutputStream getOut() {
        return out;
    }

    public OutputStream getErr() {
        return err;
    }

    public Environment getEnvironment() {
        return environment;
    }

    public void setInputStream(InputStream in) {
        this.in = in;
    }

    public void setOutputStream(OutputStream out) {
        this.out = out;
    }

    public void setErrorStream(OutputStream err) {
        this.err = err;
    }

    public void setExitCallback(ExitCallback callback) {
        this.callback = callback;
    }

    public void start(Environment env) throws IOException {
        environment = env;
        thread = new Thread(this, SHELL_THREAD_NAME);
        thread.start();
    }

    public void destroy() {
        thread.interrupt();
    }

    public void fr() throws FileNotFoundException {
        @SuppressWarnings("resource")
        Scanner input = new Scanner(new File("C:\Users\john\Desktop\asd.txt"));

        while (input.hasNextLine()) {
            System.out.println(input.nextLine());
        }

    }

    @Override
    public void run() {
        try {

            reader = new ConsoleReader(in, new FilterOutputStream(out) {
                @Override
                public void write(final int i) throws IOException {
                    super.write(i);
                }
            });
            reader.setPrompt(SHELL_PROMPT);
            reader.addCompleter(new StringsCompleter(SHELL_CMD_QUIT, SHELL_CMD_EXIT, SHELL_CMD_VERSION,
                    SHELL_CMD_HELP, SHELL_CMD_FILEREADER));
            writer = new PrintWriter(reader.getOutput());

            // output welcome banner on ssh session startup
            writer.println("****************************************************");
            writer.println("*                Welcome to aaa.                   *");
            writer.println("****************************************************");
            writer.flush();

            String line;
            while ((line = reader.readLine()) != null) {
                handleUserInput(line.trim());
            }

        } catch (InterruptedIOException e) {
            // Ignore
        } catch (Exception e) {
            e.getLocalizedMessage();
        } finally {
            callback.onExit(0);
        }
    }

    private void handleUserInput(String line) throws InterruptedIOException, FileNotFoundException {

        if (line.equalsIgnoreCase(SHELL_CMD_QUIT) || line.equalsIgnoreCase(SHELL_CMD_EXIT))
            throw new InterruptedIOException();

        String response = null;

        if (line.equalsIgnoreCase(SHELL_CMD_VERSION))
            response = "Version 1.0.0";
        else if (line.equalsIgnoreCase(SHELL_CMD_FILEREADER))
            fr();
        else if (line.equalsIgnoreCase(SHELL_CMD_HELP))
            response = "Help is not implemented yet...";
        else if (line.equalsIgnoreCase(SHELL_CMD_ENTER))
            response = "";
        else
            response = "======> \"" + line + "\" is unknown command";

        writer.println(response);
        writer.flush();
    }
}
 }

您不需要 for() 方法。

代替打印方法 fr(),这样做:

response = new String(Files.readAllBytes(Paths.get("C:\Users\John\Desktop\asd.txt")));