Esempio n. 1
0
  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());
  }
Esempio n. 2
0
  /**
   * Obtains the currently uploaded file from the session variable.
   *
   * @return uploaded file
   * @throws FileNotFoundException
   */
  private static File getCurrentUpload() throws FileNotFoundException {
    String uploadFileId = session().get("uploadFileId");
    UserUpload uploadFile = UserUpload.find.byId(Long.parseLong(uploadFileId));
    File file = new File(uploadFile.absolutePath);

    if (!file.exists()) {
      throw new FileNotFoundException("Could not load input from file.");
    }

    return file;
  }
Esempio n. 3
0
  /**
   * Helper method creates a temp file from the multipart form data and persists the upload file
   * metadata to the database
   *
   * @param filePart data from the form submission
   * @return an instance of UploadFile that has been persisted to the db
   */
  private static UserUpload uploadFile(Http.MultipartFormData.FilePart filePart) {
    File src = (File) filePart.getFile();
    File file = null;
    try {
      file = File.createTempFile(filePart.getFilename() + "-", ".csv");
      FileUtils.copyFile(src, file);
    } catch (IOException e) {
      throw new RuntimeException("Could not create temp file for upload", e);
    }

    UserUpload uploadFile = new UserUpload();
    uploadFile.absolutePath = file.getAbsolutePath();
    uploadFile.fileName = filePart.getFilename();
    uploadFile.user = Application.getCurrentUser();
    uploadFile.save();

    return uploadFile;
  }