parsing - 上传 CSV 文件并将数据解析到 Grails 中的数组?

标签 parsing grails csv upload

我创建了一个带有浏览按钮的 View ,让用户选择一个文件,然后当他们单击“上传”按钮时,它应该在 Controller 中触发该功能。

我已经使用空函数设置了 View 和 Controller ,并且我一直在查看“Grails CSV Plugin”以帮助解析文件,但从使用信息中我似乎无法掌握如何将此 CSV 数据放入数组?

任何人都可以帮我解决这个问题吗?提前致谢

这是我的 Controller ,基本上是空的:

package com.smstool

import org.springframework.dao.DataIntegrityViolationException

class UploadController {

    def index() {
        redirect(action: "upload", params: params)
    }

    def upload() {

        }

}

我的 View 仅包含此表单:

<g:form action="upload" method="post" enctype="multipart/form-data">  
            <label for="file">File:</label>  
            <input type="file" name="file" id="file"/>  
            <input class="save" type="submit" value="Upload"/>  
        </g:form>  

我刚刚在 Controller 中尝试了这个:

@Grab('com.xlson.groovycsv:groovycsv:1.0')
import static com.xlson.groovycsv.CsvParser.parseCsv

import org.springframework.dao.DataIntegrityViolationException

class UploadController {

    def index() {
        redirect(action: "upload", params: params)
    }

    def upload() {


        }


    def batchUpload() {

            def csv = new File('file')

            //def csv = '''Name,Lastname
            //Mark,Andersson
            //Pete,Hansen'''

            def data = parseCsv(csv)

            for(line in data) {
                println "$line.Number"
            }
    }



}

这不起作用,因为我收到以下错误,有什么想法吗?

Error 500: Internal Server Error

URI
    /upload/batchUpload
Class
    groovy.lang.MissingMethodException
Message
    No signature of method: static com.xlson.groovycsv.CsvParser.parseCsv() is applicable for argument types: (java.io.File) values: [file] Possible solutions: parseCsv(java.io.Reader), parseCsv(java.lang.String), parseCsv(java.util.Map, java.io.Reader), parseCsv(java.util.Map, java.lang.String), parse(java.io.Reader), parse(java.lang.String)

Around line 28 of grails-app/controllers/com/tool/UploadController.groovy

25:         //Mark,Andersson26:         //Pete,Hansen'''27:         28:         def data = parseCsv(csv)29:         30:         for(line in data) {31:              println "$line.Number"

Around line 195 of PageFragmentCachingFilter.java

192:            if (CollectionUtils.isEmpty(cacheOperations)) {193:             log.debug("No cacheable annotation found for {}:{} {}",194:                     new Object[] { request.getMethod(), request.getRequestURI(), getContext() });195:               chain.doFilter(request, response);196:              return;197:         }198:

Around line 63 of AbstractFilter.java

60:     try {61:            // NO_FILTER set for RequestDispatcher forwards to avoid double gzipping62:         if (filterNotDisabled(request)) {63:                doFilter(request, response, chain);64:          }65:            else {66:               chain.doFilter(req, res);

Trace

    Line | Method
->>   28 | batchUpload in UploadController.groovy
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    195 | doFilter    in PageFragmentCachingFilter.java
|     63 | doFilter .  in AbstractFilter.java
|   1110 | runWorker   in java.util.concurrent.ThreadPoolExecutor
|    603 | run . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    679 | run         in java.lang.Thread

最佳答案

查看:

<g:uploadForm action="upload">
    <input type="file" name="file">
    <g:submitButton name="upload" value="Upload"/>
</g:uploadForm>

Controller ,安装了 Grails CSV 插件:

def upload() {
    def file = request.getFile('file')
    def allLines = file.inputStream.toCsvReader().readAll()
    ...
}

关于parsing - 上传 CSV 文件并将数据解析到 Grails 中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12389208/

相关文章:

java - XML解析java确认

c# - 如何将字符串转换为十进制表达式?

java - GORM 中的关联、聚合、组合

java - 使用opencsv库导出数据时如何保留零开始字符串

python - 如何提取 XML 特定值字段并列出它们?

xml - 没有 XML 模式的单 channel EDI 解析 - 可能吗?

grails - GSP文件在intellij编辑器社区版中无法打开

grails - Grails 4:自定义域模型编码不适用于Grails 4

linux - 如何确定逗号分隔字符串中字符串的顺序位置?

Python Pandas : Error tokenizing data. C 错误:读取 1GB CSV 文件时字符串内的 EOF 开始