Need to compare file Id for file uploading in java bean(old file and new file)

114 views Asked by At

I am newbie in java. I need some suggestion that checking file already existing in folder if not exist then upload file else message in java.

Please give me some example

I have done this let me know how to compare old object and new object.

public Reaction updateEchemReaction(Reaction reaction) {
    LOGGER.info("updateEchemReaction(Reaction reaction) called");
    entityManager.find(Reaction.class, reaction);
    Reaction oldObject = entityManager.find(Reaction.class, reaction);
    System.out.println("oldObject==========>>"+oldObject.getFileUpload());
    try {
         //if (oldObject == null) {
         if (reaction != null && reaction.getFileUpload() != null
                && reaction.getFileUpload().size() > 0) {

            Set<UUID> fileids = filesUploadBean.updateFiles(
                    reaction.getFileUpload(), reaction.getLabId());

            Set<UUID> fileidsNew = new HashSet<>();

            fileidsNew.addAll(fileids);

            if (reaction.getFileIds() != null) {
                fileidsNew.addAll(reaction.getFileIds());
            }

            reaction.setFileIds(fileidsNew);

        }
        //}
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
}
1

There are 1 answers

3
Sheetal Mohan Sharma On

Check if folder exists && file exist or not, where filePathHere- folder to check

File f = new File(filePathHere);
if(f.exists() && !f.isDirectory()) 
   { 
   /* upload file code method and return boolean success/failure */ 

  }
else
  {
 /* return boolean success/failure or custom exception or  */ 
    }

// Compare objects - implement Comparable and implmement compareTo method, not in code below but add null checks too, check ids or whatever java class fields you like.

public int compareTo(Reaction other) {
    int i = firstField.compareTo(other.firstField);
    if (i != 0) return i;

    i = secondField.compareTo(other.secondField);
    if (i != 0) return i;

    return Integer.compare(field3, other.field3);
}