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

将文件内容显示到grails页面?,grails,groovy,Grails,Groovy,我正在我的Grails项目中构建一个特性,用户可以单击主窗体上的按钮,并使用安全FTP读取远程日志文件的内容。我要做的是将该日志文件的内容显示到Grails页面。我不知道该怎么做,网络搜索也没有结果 这是阅读日志的方法,我只是很快把它拼凑起来。不知道如何将我正在读取的文件的内容转移到grails页面。谢谢你的帮助 另外,我相信在Groovy中有一种更简单的方法来实现下面的方法 def getLogFile() { JSch jsch = new JSch(); Session

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

这是阅读日志的方法,我只是很快把它拼凑起来。不知道如何将我正在读取的文件的内容转移到grails页面。谢谢你的帮助

另外,我相信在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")

根据日志文件中的内容,这可能不是我的方法,但是,回答了您的问题。

此方法适用于文件下载。