【问题标题】:Uploading file throws No signature of method exception (in getFile() method)上传文件抛出没有方法异常的签名(在 getFile() 方法中)
【发布时间】:2013-11-13 18:18:11
【问题描述】:

我正在尝试上传文件,并将其存储在文件中。这是 GSP 中的代码:

<g:form method="post" enctype="multipart/form-data"  action="update">
<input type="file" name="cv" id="cv"/>
<g:actionSubmit action="upload" name="upload" value="Upload" />
</g:form>

在控制器中:

def upload(){
def f = request.getFile('cv')
InputStream file = f.inputStream
byte[] bytes = file.bytes
println('bytes: '+bytes)
}

正如我在标题中所说,我在这里遇到了一个例外。有什么帮助吗?谢谢。

编辑(完整堆栈跟踪,应要求):

Error 500: Internal Server Error

URI
/com.publidirecta.azafatas/azafataCertificada/index
Class
groovy.lang.MissingMethodException
Message
No signature of method:org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [cv] Possible solutions: getXML(), getPart(java.lang.String), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getJSON()

Around line 1158 of grails-app/controllers/com/publidirecta/AzafataCertificadaController.groovy

1155:           def upload(){
1156:       println("Acción upload. Params: "+params)
1157:       Azafata aza=Azafata.findByUsername(params.user)
1158:       def f = request.getFile('cv')
1159:       InputStream file = f.inputStream
1160:       byte[] bytes = file.bytes
1161:               }


Trace

    Line | Method
->> 1158 | upload  in AzafataCertificadaController.groovy
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
|    908 | run . . in     ''
^    680 | run     in java.lang.Thread

【问题讨论】:

  • 我已经尝试了代码,它对我有用,看不出有什么问题。
  • 我也没有发现任何问题,但异常不断发生。 Grails版本问题,也许?我在 2.0.4
  • 也许可以试试'grails clean'
  • 请提供完整的堆栈跟踪以确定这里发生了什么。
  • 这可能是一个配置问题,这里有一些提示可能会有所帮助:jira.grails.org/browse/GPSPRINGSECURITYCORE-191

标签: grails


【解决方案1】:

问题在于您的请求未被视为 MultiPartRequest。做这样的事情:

MultipartRequest multipartRequest =  request as MultipartRequest
  if(multipartRequest){
    MultipartFile attachmentFile = multipartRequest.getFile("attachment_file".toString())
    if (attachmentFile) {
        -- copy it ---
    }
  }

【讨论】:

    猜你喜欢
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 2013-03-18
    • 2012-11-22
    • 1970-01-01
    相关资源
    最近更新 更多