开发者

how to read only images from a folder?

开发者 https://www.devze.com 2023-03-10 18:24 出处:网络
class PhotoController { def index = { def baseFolder = grailsAttributes.getApplicationContext().getResource(\"/\").getFile().toString()
class PhotoController {
    def index = {
        def baseFolder = grailsAttributes.getApplicationContext().getResource("/").getFile().toString()
  开发者_StackOverflow      def imagesFolder = baseFolder + '/images/sps'
        def imageList1 = new File(imagesFolder).list()
        [imageList:imageList1]
    }
}

The above is listing non-jpg files too. How can I avoid that?!


You can invoke the eachFileMatch method on the folder:

def imageList1 = []
new File(imagesFolder).eachFileMatch(~/.*?\.jpg/) { imageList1 << it }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号