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

时间:2017-10-03 06:46:19

标签: java apache-mina

Hellooo我正在为服务器使用apache mina,我想制作返回一些命令的echoshellfactory。

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

我知道这是因为响应是String但是有一些方法可以使响应成为整个方法。

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();
    }
}
 }

1 个答案:

答案 0 :(得分:1)

你不需要方法fr()。

而不是打印方法fr(),请执行以下操作:

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