コード例 #1
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;
  }
コード例 #2
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;
  }