title: groovy upload file
body: <p>I'm trying to write a uploader for file and video in grails/groovy.
  So every time the user fills out a new form the controller uploads the file and saves the filename along with other fields into the database. So, here is the save procedure:</p>
  
  <pre><code>def save = {
          def streamInstance = new Stream(params)
          if(request){
              MultipartHttpServletRequest mpr = (MultipartHttpServletRequest)request;
              CommonsMultipartFile uploadedFile = (CommonsMultipartFile)mpr.getFile("myFile");
              if(!uploadedFile.empty){
                  println "helloworld"
                  println "Class: ${uploadedFile.class}"
                  println "Name: ${uploadedFile.name}"
                  println "OriginalFileName: ${uploadedFile.originalFilename}"
                  println "Size: ${uploadedFile.size}"
                  println "ContentType: ${uploadedFile.contentType}"
                  /*
                  //def webRootDir = servletContext.getRealPath("/")
                  //def userDir = new File(webRootDir, "/files/${session.user.login}")
                  //userDir.mkdirs()
                  uploadedFile.transferTo(new File("/Users/philipperibeiro/ctv/ctv/web-app/files"))
                  streamInstance.file = uploadedFile.originalFilename
                  */
              }
              else{
                  flash.message = "file cannot be empty"
              }
          }
          if(!streamInstance.hasErrors() &amp;&amp; streamInstance.save(flush : true)){
              flash.message = "${message(code: 'default.created.message', args : [message(code: 'stream.label', default:'Stream'), streamInstance.id])}"
              redirect(action : "show", id : streamInstance.id)
          }
          else{
              render(view : "create", model : [streamInstance : streamInstance])
          }
      }
  </code></pre>
  
  <p>I'm getting this error:
  Error 500: Executing action [save] of controller [ctv.StreamController] caused exception: Cannot cast object 'org.apache.catalina.core.ApplicationHttpRequest@19f46c5d' with class 'org.apache.catalina.core.ApplicationHttpRequest' to class 'org.springframework.web.multipart.MultipartHttpServletRequest'
  Servlet: grails
  URI: /ctv/grails/stream/save.dispatch
  Exception Message: Cannot cast object 'org.apache.catalina.core.ApplicationHttpRequest@19f46c5d' with class 'org.apache.catalina.core.ApplicationHttpRequest' to class 'org.springframework.web.multipart.MultipartHttpServletRequest' 
  Caused by: Cannot cast object 'org.apache.catalina.core.ApplicationHttpRequest@19f46c5d' with class 'org.apache.catalina.core.ApplicationHttpRequest' to class 'org.springframework.web.multipart.MultipartHttpServletRequest' 
  Class: StreamController 
  At Line: [22] 
  Code Snippet:</p>
  
  <p>could someone give a clue of who to fix it?</p>
  
  <pre><code>&lt;%@ page import="ctv.Stream" %&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
          &lt;meta name="layout" content="main" /&gt;
          &lt;g:set var="entityName" value="${message(code: 'stream.label', default: 'Stream')}" /&gt;
          &lt;title&gt;&lt;g:message code="default.create.label" args="[entityName]" /&gt;&lt;/title&gt;
          &lt;jqfu:resources tmpl=”false” /&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div class="nav"&gt;
              &lt;span class="menuButton"&gt;&lt;a class="home" href="${createLink(uri: '/')}"&gt;&lt;g:message code="default.home.label"/&gt;&lt;/a&gt;&lt;/span&gt;
              &lt;span class="menuButton"&gt;&lt;g:link class="list" action="list"&gt;&lt;g:message code="default.list.label" args="[entityName]" /&gt;&lt;/g:link&gt;&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="body"&gt;
              &lt;h1&gt;&lt;g:message code="default.create.label" args="[entityName]" /&gt;&lt;/h1&gt;
              &lt;g:if test="${flash.message}"&gt;
              &lt;div class="message"&gt;${flash.message}&lt;/div&gt;
              &lt;/g:if&gt;
              &lt;g:hasErrors bean="${streamInstance}"&gt;
              &lt;div class="errors"&gt;
                  &lt;g:renderErrors bean="${streamInstance}" as="list" /&gt;
              &lt;/div&gt;
              &lt;/g:hasErrors&gt;
              &lt;g:form action="save" &gt;
                  &lt;div class="dialog"&gt;
                      &lt;table&gt;
                          &lt;tbody&gt;
  
                              &lt;tr class="prop"&gt;
                                  &lt;td valign="top" class="name"&gt;
                                      &lt;label for="district"&gt;&lt;g:message code="stream.district.label" default="District" /&gt;&lt;/label&gt;
                                  &lt;/td&gt;
                                  &lt;td valign="top" class="value ${hasErrors(bean: streamInstance, field: 'district', 'errors')}"&gt;
                                      &lt;g:select name="district.id" from="${ctv.District.list()}" optionKey="id" value="${streamInstance?.district?.id}"  /&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
  
                              &lt;tr class="prop"&gt;
                                  &lt;td valign="top" class="name"&gt;
                                      &lt;label for="dateAdded"&gt;&lt;g:message code="stream.dateAdded.label" default="Date Added" /&gt;&lt;/label&gt;
                                  &lt;/td&gt;
                                  &lt;td valign="top" class="value ${hasErrors(bean: streamInstance, field: 'dateAdded', 'errors')}"&gt;
                                      &lt;g:datePicker name="dateAdded" precision="day" value="${streamInstance?.dateAdded}"  /&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
  
                              &lt;tr class="prop"&gt;
                                  &lt;td valign="top" class="name"&gt;
                                      &lt;label for="name"&gt;&lt;g:message code="stream.name.label" default="Name" /&gt;&lt;/label&gt;
                                  &lt;/td&gt;
                                  &lt;td valign="top" class="value ${hasErrors(bean: streamInstance, field: 'name', 'errors')}"&gt;
                                      &lt;g:textField name="name" maxlength="30" value="${streamInstance?.name}" /&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
  
                              &lt;tr class="prop"&gt;
                                  &lt;td valign="top" class="name"&gt;
                                      &lt;label for="description"&gt;&lt;g:message code="stream.description.label" default="Description" /&gt;&lt;/label&gt;
                                  &lt;/td&gt;
                                  &lt;td valign="top" class="value ${hasErrors(bean: streamInstance, field: 'description', 'errors')}"&gt;
                                      &lt;g:textField name="description" maxlength="100" value="${streamInstance?.description}" /&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
  
                              &lt;tr class="prop"&gt;
                                  &lt;td valign="top" class="name"&gt;
                                      &lt;label for="media"&gt;&lt;g:message code="stream.media.label" default="Media" /&gt;&lt;/label&gt;
                                  &lt;/td&gt;
                                  &lt;td valign="top" class="value ${hasErrors(bean: streamInstance, field: 'media', 'errors')}"&gt;
                                      &lt;g:textField name="media" maxlength="30" value="${streamInstance?.media}" /&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
  
                              &lt;tr class="prop"&gt;
                                  &lt;td valign="top" class="name"&gt;
                                      &lt;label for="file"&gt;&lt;g:message code="stream.file.label" default="File" /&gt;&lt;/label&gt;
                                  &lt;/td&gt;
                                  &lt;td valign="top" class="value ${hasErrors(bean: streamInstance, field: 'file', 'errors')}"&gt;
                                      &lt;!--g:textField name="file" maxlength="30" value="${streamInstance?.file}" /&gt;--&gt;
                                      &lt;g:form action="save"  enctype="multipart/form-data"&gt;
                                          &lt;input type="file" name="myFile" /&gt;
                                      &lt;/g:form&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
  
                              &lt;tr class="prop"&gt;
                                  &lt;td valign="top" class="name"&gt;
                                      &lt;label for="logo"&gt;&lt;g:message code="stream.logo.label" default="Logo" /&gt;&lt;/label&gt;
                                  &lt;/td&gt;
                                  &lt;td valign="top" class="value ${hasErrors(bean: streamInstance, field: 'logo', 'errors')}"&gt;
                                      &lt;g:textField name="logo" value="${streamInstance?.logo}" /&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
  
                              &lt;tr class="prop"&gt;
                                  &lt;td valign="top" class="name"&gt;
                                      &lt;label for="published"&gt;&lt;g:message code="stream.published.label" default="Published" /&gt;&lt;/label&gt;
                                  &lt;/td&gt;
                                  &lt;td valign="top" class="value ${hasErrors(bean: streamInstance, field: 'published', 'errors')}"&gt;
                                      &lt;g:checkBox name="published" value="${streamInstance?.published}" /&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
  
                          &lt;/tbody&gt;
                      &lt;/table&gt;
                  &lt;/div&gt;
                  &lt;div class="buttons"&gt;
                      &lt;span class="button"&gt;&lt;g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" /&gt;&lt;/span&gt;
                  &lt;/div&gt;
              &lt;/g:form&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>