Groovy - File handling from http URL

前端 未结 4 1841
旧巷少年郎
旧巷少年郎 2021-01-03 12:32

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:



        
4条回答
  •  孤街浪徒
    2021-01-03 13:32

    No, you'll need to do some parsing of the returned HTML.

    Given this page as an example: http://central.maven.org/maven2/com/bloidonia/groovy-stream/

    We'd need to do something like:

    @Grab( 'org.ccil.cowan.tagsoup:tagsoup:1.2.1' )
    
    def url = 'http://central.maven.org/maven2/com/bloidonia/groovy-stream/'.toURL()
    
    new XmlSlurper( new org.ccil.cowan.tagsoup.Parser() ).parseText( url.text )
                                                         .body
                                                         .pre
                                                         .a
                                                         .each { link ->
        if( link.@href.text().endsWith( '/' ) ) {
            println "FOLDER : ${link.text()}"
        }
        else {
            println "FILE   : ${link.text()}"
        }
    }
    

    Which prints out:

    FOLDER : ../
    FOLDER : 0.5.1/
    FOLDER : 0.5.2/
    FOLDER : 0.5.3/
    FOLDER : 0.5.4/
    FOLDER : 0.6/
    FOLDER : 0.6.1/
    FOLDER : 0.6.2/
    FILE   : maven-metadata.xml
    FILE   : maven-metadata.xml.md5
    FILE   : maven-metadata.xml.sha1
    

    Obviously, you'd need to tweak the body.pre.a bit to match the output of your webserver for directory listings

提交回复
热议问题