Grails request.getFile在Jetty中工作,但在部署到Glassfish 3.1.2服务器时返回Null

时间:2012-06-15 14:00:46

标签: grails groovy glassfish jetty

我使用Grails 2.0.4构建了我的应用程序并在Jetty中进行了测试。在Jetty中一切正常,但是当我部署到Glassfish时,我尝试在我的操作中分配文件时得到一个npe。为什么在glassfish上uploadFile为null但是使用jetty呢?

def  uploadedFile = request.getFile('name_of_file')

Firebug显示http请求正在获取该文件。以下是来自Firebug的多部分表单数据Post请求的开头:

**name_of_file**    %PDF-1.4 %âãÏÓ 5 0 obj <</Length 2712/Filter/FlateDecode>>stream x[]oÜ6}_A /ÛÝZD}öM+¶ñÌT#'5¶ÂuÄeolÅþüÔ¥D^êòÑ(PÏuyι<¤4²úuuÚ¬|æ!k>®ÊfõëêëÊ÷|ÆìïUÈ~ÿñ

因此,在部署时看起来文件信息在客户端进行了参数化,但是没有进入控制器。

我的控制器代码:

 def sample(){

    if (params.ID == null) return 

    def sampleInstance = new Sample(params)// new sample
    if (sampleInstance.validate()) { // try to validate
        def  uploadedFile = request.getFile('name_of_file')// see if there is a file to upload
        session.uploadedFile = [
            originalFile: uploadedFile.originalFilename
        ]



        if (uploadedFile != null) { // is there a file? 
            sampleInstance.filepath =${uploadedFile.originalFilename}" // save the original filename

        }
        if (params.ageUnk){
           sampleInstance.sampleAge = null;
        }
        if (!params.ageUnk){
        def patients_age_in_years = (Integer.parseInt(params.patientYrs) * 12)

        def patients_age_in_months = Integer.parseInt(params.patientMos)

        def patients_total_age_in_months = (patients_age_in_years + patients_age_in_months) 

        sampleInstance.sampleAge = patients_total_age_in_months
        }

        sampleInstance.enteredby = springSecurityService.currentUser.id 
        sampleInstance.save()// save the sample in the database

        redirect( action:'sample', model:[sampleInstance:sampleInstance, checkDate:today, date: now])

    }else{
         [sampleInstance:sampleInstance, checkDate:today, date: now]
    }

}

除文件外,所有参数都能通过。 这是我的gsp表单:

<g:uploadForm action="sample">
<table>
  <tr>
    <td align="right"><br><b> Comments: &nbsp</b></td>
    <td><br><g:textField name="nameComments" value="" style='width: 200px'/></td>
  </tr>
    <td align="right"><br><b>ID: &nbsp</b></td>
    <td><br><g:textField name="nameID" value="" /></td>
  </tr>
  <tr>
    <td align="right"><br><b>Upload File: &nbsp</b></td>
    <td><br><input type='file' name='name_of_file' /></td>
  </tr>
  <tr>
    <td align="right"></td>
    <td><g:submitButton name="submit" value="Submit" /></td>
  </tr>
</table> 

</g:uploadForm>

堆栈跟踪:

java.lang.NullPointerException: Cannot get property 'originalFilename' on null object 
at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:56) 
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:156) 
at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:44) 
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:227) 
at lims.SubmitSampleController.sample(SubmitSampleController.groovy:27) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at org.codehaus.groovy.grails.web.servlet.mvc.MixedGrailsControllerHelper.invoke(MixedGrailsControllerHelper.java:67) 
at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleAction(AbstractGrailsControllerHelper.java:330) 
at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.executeAction(AbstractGrailsControllerHelper.java:211) 
at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:177) 
at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:116) 
at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController.handleRequest(SimpleGrailsController.java:72) 
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48) 
at org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet.doDispatch(GrailsDispatcherServlet.java:325) 
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827) 
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) 
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:688) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770) 
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) 
at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:807) 
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:671) 
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:505) 
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:476) 
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:355) 
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:305) 
at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:312) 
at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:277) 
at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:268) 
at org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter.doFilterInternal(UrlMappingsFilter.java:209) 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) 
at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.obtainContent(GrailsPageFilter.java:200) 
at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.doFilter(GrailsPageFilter.java:151) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:369) 
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109) 
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381) 
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381) 
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381) 
at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:119) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381) 
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381) 
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187) 
at org.codehaus.groovy.grails.plugins.springsecurity.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:40) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381) 
at org.codehaus.groovy.grails.plugins.springsecurity.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:79) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381) 
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381) 
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:168) 
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) 
at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:69) 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 
at org.apache.catalina. 

1 个答案:

答案 0 :(得分:0)

显然这是Glassfish 3.1.2的一个问题。我今天早上发现Tweet关于我的问题导致我JIRA

替换web-core.jar文件为我解决了问题。为了概念验证,我下载了Glassfish 3.1.1并在该版本上运行我的应用程序,没有任何问题。因此,所有事情都指向3.1.2是我问题的根本原因。我的应用中没有任何内容。