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

时间:2012-09-12 13:23:44

标签: parsing grails csv upload

我创建了一个带有浏览按钮的视图,让用户选择一个文件,然后当他们点击上传按钮时,它应该在控制器中触发该功能。

我已经使用空函数设置了视图和控制器,我一直在查看“Grails CSV Plugin”以帮助解析文件但是从使用信息中我似乎无法掌握如何获取此CSV数据变成数组?

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

这是我的控制器基本上是空的:

package com.smstool

import org.springframework.dao.DataIntegrityViolationException

class UploadController {

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

    def upload() {

        }

}

我的观点只包含这种形式:

<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>  

我刚在控制器中试过这个:

@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

2 个答案:

答案 0 :(得分:3)

查看:

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

控制器,安装了Grails CSV插件:

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

答案 1 :(得分:0)

读取单行或第一行:

def upload() {
   def file = request.getFile('file')
   def aLine = file.inputStream.toCsvReader().readNext()
   ...
}