将文件的内容显示到 grails 页面?

Displaying the contents of a file to a grails page?

我正在我的 Grails 项目中构建一个功能,用户可以单击主窗体上的按钮并使用安全 FTP 它将读取远程日志文件的内容。我想要做的是将该日志文件的内容显示到 Grails 页面。我不知道该怎么做,网上搜索也没有结果。

这是阅读日志的方法,我很快就把它放在一起了。不确定如何处理我正在阅读的文件的内容并将其转储到 grails 页面。任何帮助表示赞赏。

P.S。我确定在 Groovy.

中有更简单的方法来执行下面的方法
 def getLogFile() {

    JSch jsch = new JSch();
    Session session = null;

    try {

        String username = "joesmith"
        String password = "mypassword"
        String hostname = "123.456.78.910"
        String x
        // connect to the server through secure ftp
        session = jsch.getSession(username, hostname, 22);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword(password);
        session.connect();
        ChannelSftp channel = (ChannelSftp)session.openChannel("sftp");
        channel.connect();
        log.info("Session Connected, reading log file...");

        ChannelSftp sftpChannel = (ChannelSftp) channel;
        sftpChannel.cd("/usr/tmp")
        java.io.InputStream stream = sftpChannel.get("mylog.txt");

        BufferedReader br = new BufferedReader(new InputStreamReader(stream));

        while ((x = br.readLine()) != null) {
            log.info("line is " + x)
        }
    } catch (JSchException e) {
        e.printStackTrace();
    } catch (SftpException e) {
        e.printStackTrace();
    }

有几种不同的方法可以做到这一点,有些方法比其他方法更安全。如果您的日志文件太大,您将无法显示它们。您还可以考虑编码和安全问题。

最简单快捷的方法就是使用 render 方法将字符串转储到控制器调用内的页面:

    def stringBuilder = new StringBuilder()
    while ((x = br.readLine()) != null) {
        stringBuilder.append(x)
    }
    render(text: stringBuilder.toString(), contentType: "text/plain", encoding: "UTF-8")

这可能不是我的方法,具体取决于日志文件中的内容,但可以回答您的问题。