Grails文件上传问题 [英] Grails File Upload Problems

查看:354
本文介绍了Grails文件上传问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图模仿Grails网站上的文件上传代码,我遇到了一些问题。我使用与此处相同的代码。这里是我的代码:

 < g:form action =uploadmethod =postenctype =multipart / form -data> 
< input type =filename =myFile/>
< input type =submitvalue =上传/>
< / g:表格>

  def upload = {
def f = request.getFile('myFile')
if(!f.empty){
flash.message ='success'
}
else {
flash.message ='文件不能为空'
}
}

我在运行时收到以下错误:

 消息:方法没有签名:org.mortbay.jetty.Request.getFile()适用于参数类型:(java.lang.String)values:{myFile} 
引起:groovy.lang.MissingMethodException:方法没有签名: org.mortbay.jetty.Request.getFile()适用于参数类型:(java.lang.String)values:{myFile}

这似乎与一些Spring配置有关。 Spring似乎没有注入 MultipartHttpServletRequest ,所以我的请求没有适当的方法。我刚刚使用 grails create-app 创建了这个应用程序。我没有修改resources.groovy文件。我正在使用grails 1.0.3。

任何帮助都非常感谢。这个grails网站让这个看起来很简单。

解决方案



<我正在使用示例代码来上传文件到Grails,而不是原来的作者可能的意图。问题是,当调用控制器的 upload 方法时,有时是上传页面的原始呈现。该方法中的请求不是MultipartHttpServletRequest类型的请求。当我用我的文件上传POST时,Spring做了正确的事情,并将我的请求改为MultipartHttpServletRequest。因此,在使用我的请求(如MultipartHttpServletRequest)之前,我需要在我的 update 控制器方法中进行一次简单的检查。

 <$ c (请求instanceof MultipartHttpServletRequest)
{
MultipartHttpServletRequest mpr =(MultipartHttpServletRequest)请求;
CommonsMultipartFile f =(CommonsMultipartFile)mpr.getFile(myFile);
if(!f.empty)
flash.message ='success'
else
flash.message ='文件不能为空'
}
else
flash.message ='请求的类型不是MultipartHttpServletRequest'


I'm trying to emulate the file upload code from the grails website, and I'm running into some problems. I'm using the same code as found here. Here is my code:

    <g:form action="upload" method="post" enctype="multipart/form-data">
        <input type="file" name="myFile" />
        <input type="submit" value="Upload" />
    </g:form>

and

def upload = {
    def f = request.getFile('myFile')
    if(!f.empty) {
      flash.message = 'success'
    }    
    else {
       flash.message = 'file cannot be empty'
    }
}

I'm receiving the following error at runtime:

Message: No signature of method: org.mortbay.jetty.Request.getFile() is applicable for argument types: (java.lang.String) values: {"myFile"}
Caused by: groovy.lang.MissingMethodException: No signature of method: org.mortbay.jetty.Request.getFile() is applicable for argument types: (java.lang.String) values: {"myFile"}

It appears to be related to some Spring configuration. Spring does not appear to be injecting MultipartHttpServletRequest, so my request doesn't have the appropriate method. I just created this applications using grails create-app. I have not modified the resources.groovy file. I'm using grails 1.0.3.

Any help is much appreciated. The grails website makes this look so easy.

解决方案

Problem solved!

I was using the example code for uploading files to Grails differently than the original author probably intended. The problem is that when the upload method of the controller was called, it was sometimes for the original render of the Upload page. The request in that method was was not of type MultipartHttpServletRequest. When I did a POST with my file to upload, then Spring did the correct thing and changed my requestion to MultipartHttpServletRequest. So, I needed to do a simple check in my update controller method before using my request like a MultipartHttpServletRequest.

if(request instanceof MultipartHttpServletRequest)
{
  MultipartHttpServletRequest mpr = (MultipartHttpServletRequest)request;  
  CommonsMultipartFile f = (CommonsMultipartFile) mpr.getFile("myFile");
  if(!f.empty)
    flash.message = 'success'
  else
   flash.message = 'file cannot be empty'
}   
else
  flash.message = 'request is not of type MultipartHttpServletRequest'

这篇关于Grails文件上传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆