public static void createAd(@Valid Ad ad, File photo) throws IOException { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // get current date time with Date() Date date = new Date(); ad.createDate = dateFormat.format(date); ad.student = Student.findById(1l); // ad.category=Category.findById(ad.category.id); File d = new File(Play.applicationPath.getAbsolutePath() + "/public/img/ads"); // if(d.exists()){ String suffix = FilenameUtils.getExtension(photo.getName()); File o = File.createTempFile("ad-", "." + suffix, d); InputStream input = new FileInputStream(photo); OutputStream output = new FileOutputStream(o); ad.image = o.getName(); ad.save(); try { IOUtils.copy(input, output); } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(input); } Ads.index(1, ad.category.id.toString()); }