Esempio n. 1
0
  /**
   * Remove file on server by researcher get from parameter method.
   *
   * @param fileRP
   * @param researcher
   */
  public void remove(EmbeddedFile fileRP) {
    File directory = new File(getBasePath() + File.separatorChar + fileRP.getFolderFile());

    if (directory.exists()) {
      String fileName = fileRP.getValueFile();
      if (fileRP.getExtFile() != null && !fileRP.getExtFile().isEmpty()) {
        fileName += ("." + fileRP.getExtFile());
      }
      Collection<File> files = FileUtils.listFiles(directory, new WildcardFilter(fileName), null);

      for (File file : files) {
        file.delete();
      }
    }
  }
Esempio n. 2
0
  /**
   * Load file and copy to default directory.
   *
   * @param file
   */
  public EmbeddedFile load(
      InputStream stream,
      String originalFilename,
      String contentType,
      String extAuthority,
      String intAuthority)
      throws IOException, FileNotFoundException {
    EmbeddedFile result = new EmbeddedFile();
    String folder = intAuthority;
    if (extAuthority != null && extAuthority.length() > 0) {
      folder = getCustomFolderByAuthority(extAuthority, intAuthority);
    }
    String fileName = "";
    String pathCV = getBasePath();
    String ext = null;
    File dir = new File(pathCV + File.separatorChar + folder);
    dir.mkdirs();
    File file = null;
    if (originalFilename.lastIndexOf(".") == -1) {
      fileName = originalFilename;
      file = new File(dir, fileName);
    } else {
      fileName = originalFilename.substring(0, originalFilename.lastIndexOf("."));
      ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
      file = new File(dir, fileName + "." + ext);
    }

    int i = 1;
    while (!file.createNewFile()) {
      fileName = fileName + "_" + i;
      file = new File(dir, fileName + ((ext != null) ? ("." + ext) : ""));
      i++;
    }

    FileOutputStream out = new FileOutputStream(file);
    it.cilea.osd.common.util.Utils.bufferedCopy(stream, out);
    out.close();
    result.setValueFile(fileName);
    result.setExtFile(ext);
    result.setMimeFile(contentType);
    result.setFolderFile(folder);
    return result;
  }