Loading an image to the filesystem in Grails

I am implementing a file upload function to a web application in Grails. This includes adapting existing code to allow for multiple file extensions. In the code I have implemented a boolean to check if the file path exists, but I still get a FileNotFoundException that/hubbub/images/testcommand/photo.gif (No such file or directory)

My download code

def rawUpload = {       
    def mpf = request.getFile("photo")
    if (!mpf?.empty && mpf.size < 200*1024){
        def type = mpf.contentType
        String[] splitType = type.split("/")

        boolean exists= new File("/hubbub/images/${params.userId}")

        if (exists) {
            mpf.transferTo(new File("/hubbub/images/${params.userId}/picture.${splitType[1]}"))
        } else {
            tempFile = new File("/hubbub/images/${params.userId}").mkdir()
            mpf.transferTo(new File("/hubbub/images/${params.userId}/picture.${splitType[1]}"))
        }

    }
}

      

I am getting an error about

if (exists) {
        mpf.transferTo(new File("/hubbub/images/${params.userId}/picture.${splitType[1]}"))
}

      

So why is this error happening as I am just matching a valid existing path as well as a valid filename and extension?

+3


source to share


1 answer


Why do you think converting an object File

to Boolean

returns the existence of a file?

Try



    File dir = new File("/hubbub/images/${params.userId}")
    if (!dir.exists()) {
        assert dir.mkdirs()
    }
    mpf.transferTo(new File(dir, "picture.${splitType[1]}"))

      

+5


source







All Articles