I would like to have the user upload a file inside a form and upon submission, open the file and read each line in order to grab information from it to store in the database. So far I am only able to make an object and extract bytes from it.
def uploadedFile = request.getFile('name_of_file')
This gets me a commonsMultipartFile, but I am not sure if I need to stream the file into a buffer, or save the file into a temp folder and then try to open it that way.
uploadedFile.getbytes()
will give me an array [71, 101, 110, 101, 114, 97, 108, 13, 10, 45, 45, 45, 45, 45, 45, 45, 13, 10, 83, 116....] which is not in a form I can do anything with.
I need to be able to open the file and read it line by line. Can I do that with the commonsMultipartFile - its options seem kind of limited.
The CommonsMultipartFile
has a getInputStream() method which will return an InputStream
on which you can call any Groovy enhancements, for example:
request.getFile('name_of_file').inputStream.eachLine { line ->
/* Parsing logic */
}