有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ejb需要比较JavaBean中文件上传的文件Id(旧文件和新文件)

我是java的新手。我需要一些建议,检查文件夹中已经存在的文件,如果不存在,然后用java上传file else消息

请给我举个例子

我这样做让我知道如何比较旧对象和新对象

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) 个答案

  1. # 1 楼答案

    检查文件夹是否存在&&;文件是否存在,在哪里 filePathHere-要检查的文件夹

    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  */ 
        }
    

    //比较对象-实现Comparable和ImplementCompareTo方法,不是在下面的代码中,而是添加空检查、检查ID或任何您喜欢的java类字段

    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);
    }