我已经使用curl命令将CSV文件发送给我的后端构建3.1.1,我可以在Multipart中获取文件。现在,我想访问该文件,并希望根据该CSV文件中的信息更新数据库。因此,是否有任何插件可以为Grails 3.1.1解析此类文件,如果没有任何此类插件,那么有人可以使用示例的Groovy代码来解析此类文件。我曾经尝试过,但我没有这样做?
我使用https://bintray.com/sachinverma/plugins/org.grails.plugins:csv从那里的文档中获取Grails 3项目的插件并不明显,因此将以下内容添加到build.gradle:
repositories {
https://bintray.com/sachinverma/plugins/org.grails.plugins:csv
}
dependencies {
compile "org.grails.plugins:csv:1+"
}
然后使用:
def is
try {
def file = params.csvfile
is = file.getInputStream()
is.toCsvReader().eachLine { tokens ->
do stuff with tokens
}
...
或
def is
try {
is = params.csvfile.getInputStream()
def csvMapReader = new CSVMapReader( new InputStreamReader( is ) )
csvMapReader.fieldKeys = ["field1","field2"]
csvMapReader.eachWithIndex { map, idx ->
do stuff with map
}
...