简体   繁体   English

如何使用 Groovy 从服务器上的给定路径读取文件名

[英]How to read file names from a given path on server using Groovy

I have multiple files on the given FTP path.我在给定的 FTP 路径上有多个文件。 I want to read the latest file received, for which I need to read all the file names in the given path.我想读取收到的最新文件,为此我需要读取给定路径中的所有文件名。 I am using Groovy on the OUAF based product, ORMB.我在基于 OUAF 的产品 ORMB 上使用 Groovy。 Please suggest on how to read the file names.请建议如何读取文件名。 Also, new File() statement is not supported.此外,不支持 new File() 语句。

Assuming you want to use apache commons.net module you can do this:假设你想使用 apache commons.net 模块,你可以这样做:

@Grab(group='commons-net', module='commons-net', version='3.7')
import org.apache.commons.net.ftp.FTPClient

String hostname = "some-server.some-domain.com"
String username = "someUser"
String password = "somePassword"
String path = "/var/some/path"

println("About to connect....");
new FTPClient().with {
    connect hostname
    enterLocalPassiveMode()
    login username, password
    changeWorkingDirectory path
    FTPFile latest = listFiles().max { it.timestamp.time }
    println("Downloading ${latest.name}..")
    File incomingFile = new File( latest.name )
    incomingFile.withOutputStream { os -> retrieveFile(latest, os) }
    disconnect()
}
println("...Done.");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM