使用grails 2.3.7上传时将错误视为invalidFormatException

时间:2014-04-30 10:51:11

标签: grails groovy

DocumentController.groovy

def upload() {
              def file =request.getFile('file')
              def excelImportService
              XSSFWorkbook book = new XSSFWorkbook(file.getInputStream());
              Map CONFIG_BOOK_COLUMN_MAP = [
             sheet:'Sheet1',
             startRow: 1,
             columnMap:  [
              //Col, Map-Key
              'A':'Question',
              'B':'SecType',
              'C':'Option1',
              'D':'Option2',
              'E':'Option3',
              'F':'Option4',
              'G':'CorrectAnswer',
              'H':'QuestionOrder'
             ]
            ]
         //Iterate through bookList and create/persists your domain instances
          def bookList = excelImportService.columns(book, CONFIG_BOOK_COLUMN_MAP)

使用相同格式上传excel文件时获取错误:

2014-04-30 15:49:31,024 [http-bio-8080-exec-9] ERROR errors.GrailsExceptionResolver  - InvalidFormatException occurred when processing request: [POST] /Application/document/upload - parameters:**
    upload: Upload                    
    Package should contain a content type part [M1.13]. Stacktrace follows:
    Message: Package should contain a content type part [M1.13]
        Line | Method

1 个答案:

答案 0 :(得分:0)

而不是:

XSSFWorkbook book = new XSSFWorkbook(file.getInputStream());

您应该使用:

Workbook book = WorkbookFactory.create(file.inputStream);

def excelImportService

应该从upload()方法中移除,而是将其作为DocumentController类的属性