title: Play framework upload file not working
body: <p>I wanted to upload a CSV file and from there get the details in order to fill up a MySQL database. I am using Play! framework 2 with Java but i am having difficulties to implement this solution.</p>
  
  <p>This is my Java code</p>
  
  <pre><code>       public static Result uploadfile() {
           File fl = request().body().asRaw().asFile();
  
            if (fl != null) {
  
  
              return ok("File uploaded");
            } else {
              flash("error", "Missing file");
              return redirect(routes.Application.index());    
            }
  </code></pre>
  
  <p>This is my Upload.Scala.html code</p>
  
  <pre><code>   @(uploadForm: Form[Application.upl])
  
     @import helper._
  
     @main("Upload file") {
        @form(action = routes.Application.uploadfile) {
  
           &lt;input type="file" name="flname"&gt;
  
           &lt;p&gt;
              &lt;input type="submit" value="upload"&gt;
           &lt;/p&gt;
  
        }
     }
  </code></pre>
  
  <p>And finally my routes file </p>
  
  <pre><code>POST     /uploadfile                controllers.Application.uploadfile()
  </code></pre>
  
  <p>Once I hit upload an execution exception occurs saying that the <code>request().body().asRaw().asFile()</code> is actually <code>null</code>! </p>
  
  <blockquote>
    <p>[NullPointerException: null]</p>
  </blockquote>
  
  <p>Any ideas what I am doing wrong?</p>
  
  <p>===================================================================================
  Ok so my current code is as follows
  On application.java</p>
  
  <pre><code>  public static Result uploadfile() {
    play.mvc.Http.MultipartFormData body = request().body().asMultipartFormData();
      FilePart flpart = body.getFile("fl") ;
      if (flpart != null) {
  
         File file = flpart.getFile();
         ImportCSV impCSV = new ImportCSV();
         try{
             if (file.exists()) 
             {
                 BufferedReader br = new BufferedReader( new FileReader(file));
                 System.out.println(file.getName() + " - " + file.getAbsolutePath());
                 impCSV.importCSV(br);
             }
             else
             {
                 System.out.println("File not found!");
                 return redirect(routes.Application.upload()); 
             }
         }
         catch(Exception e)
         {
             System.err.println("Buffering file " + file.getName() +": " + e.getMessage());
  
         }
         return ok("File uploaded");
      } else {
         flash("error", "Missing file");
       return redirect(routes.Application.index());    
      }
  }
  </code></pre>
  
  <p>And on the ImportCSV.java (which extends model)</p>
  
  <pre><code>public static void importCSV( BufferedReader br) 
  {
  
      parseFile(br);
  
  
      if(vals!=null)
      {
          Student student = new Student();
          List&lt;Student&gt; newStudents;
          newStudents = GetStudentDetails(vals);
          try{
              student.insertStudentList(newStudents);
          }
          catch(Exception e)
          {
              System.err.println("student inserting Error: " + e.getMessage());
  
          }
  
      }
  }
  
   static void parseFile( BufferedReader br) {
      try {
  
            String line = "";
            StringTokenizer token = null;
            int lineNum = 0, tokenNum = 0;
  
            List&lt;values&gt; CSVvalues = new LinkedList&lt;values&gt;();
            values LnValues = new values();
            while((line = br.readLine()) != null) {
                  lineNum++;
  
                  // break comma separated file line by line
                  token = new StringTokenizer(line, ",");
                  LnValues = new values();
                  while(token.hasMoreTokens()) {
                        tokenNum++;
                        switch(tokenNum)
                        {
                          //Student ID
                            case 0:
                            {
                                LnValues.setStudID(Long.getLong(token.nextToken()));
                            }
                            //Student Name
                            case 1:
                            {
                                LnValues.setStudName(token.nextToken());
                            }
                            //Student DoB
                            case 2:
                            {
                                LnValues.setStudDoB(Date.valueOf(token.nextToken()));
                            }
                            //Class
                            case 3:
                            {
                                LnValues.setCl(token.nextToken());
                            }
                            //Year
                            case 4:
                            {
                                LnValues.setYear(Integer.parseInt(token.nextToken()));
                            }
                            //Quarter
                            case 5:
                            {
                                LnValues.setQuarter(token.nextToken());
                            }
                            //Maths Grade
                            case 6:
                            {
                                LnValues.setGradeM( Float.parseFloat(token.nextToken()));
                            }
                            //Computer Science Grade
                            case 7:
                            {
                                LnValues.setGradeCS(Float.parseFloat(token.nextToken()));
                            }
                            //Literature grade
                            case 8:
                            {
                                LnValues.setGradeL( Float.parseFloat(token.nextToken()));
                            }                           
  
                        }
  
  
                  }
                  CSVvalues.add(LnValues);
                  tokenNum = 0;
            }
  
            vals = CSVvalues;
      } catch(Exception e) {
            System.err.println("Parsing file  Error: " + e.getMessage());
  
      }
  </code></pre>
  
  <p>Once i pass the Buffered reader the data are null. At the beginning i tried to send the file (File fl) to the parameters but i also got null error. I tried @Util annotation shown in some cases but the compiler complains for not recognizing it! 
  I am open to suggestions!  </p>