Groovy - 从http URL进行文件处理 [英] Groovy - File handling from http URL
问题描述
我们的其中一台服务器中的文件可以通过http访问。所以,当我们调出一个类似于下面的url时,我们得到了该位置的文件/目录列表:
http :// mytestserver / files /
从这个列表中,我只需要选择那些匹配正则表达式格式。
如果这是磁盘中的一个位置,我可以使用eachFileMatch方法并筛选我需要的文件。
有人可以帮助我如何从http URL做到这一点吗?
@tim_yates的另一个版本使用 JSoup
<$
$ or $ $ $
$ (doc,files,dirs)= [Jsoup.connect('http://central.maven.org/maven2/com/bloidonia/groovy-stream/').get(),[],[]]
doc.select(pre> a)。each {href - >
def filename = href.text()
filename.endsWith(/)?dirs.add(filename):files.add(filename)
}
println DIRECTORIES:
$ {dirs.join('\ n')}
文件:
$ {files.join('\\\
')}
The files in one of our servers can be accessed via http. So, when we bring up a url similar to the following, we get a list of files/directories in that location:
http://mytestserver/files/
From this list, I need to select only those files that match a regex format.
If this was a location in the disk, I can make use of the method eachFileMatch and filter the files that I need.
Can someone help me how we can do this from a http URL?
Another version of @tim_yates Answer using JSoup
@Grab(group='org.jsoup', module='jsoup', version='1.7.3')
import org.jsoup.Jsoup
def (doc,files, dirs) = [Jsoup.connect('http://central.maven.org/maven2/com/bloidonia/groovy-stream/').get(),[],[]]
doc.select("pre > a").each{href ->
def filename = href.text()
filename.endsWith("/")?dirs.add(filename):files.add(filename)
}
println """DIRECTORIES :
${dirs.join('\n')}
FILES :
${files.join('\n')}
"""
这篇关于Groovy - 从http URL进行文件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!