java比較兩個遠程文件內容是否相同

java比較兩個遠程文件內容是否相同

最近利用active mq在做兩個系統之間的數據同步,包括文件的同步。
如果重複同步肯定要判斷兩個文件是否一致
按照百度的方法試了試

第一種方法在某些情況下是可以的,第二種完全是比較內容更可靠一點

第一種利用MD5比較

/*
     * 根據文件路徑獲取md5的值
     * @param filePath
     * @return
     */
    public static String getFileMD5(String path) throws IOException {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream in = conn.getInputStream();
        String md5Hex = DigestUtils.md5Hex(in);
        return md5Hex;
    }


 /**
     * 比較兩個文件的大小
     *
     * @param path
     * @param newPath
     * @return
     * @throws IOException
     */
    public static Boolean compareFile(String path, String newPath) throws IOException {
        if (StringUtils.isBlank(path) || StringUtils.isBlank(newPath)) {
            return false;
        }
        String fileMD5 = getFileMD5(path);
        String newFileMD5 = getFileMD5(newPath);
        if (fileMD5.equals(newFileMD5)) {
            return true;
        } else {
            return false;
        }
    }

測試運行

 Boolean aBoolean = compareFile("http://117.71.53.32:19559/fileServices/resources/test_images/image/20200512/1589262909067.png", "http://117.71.53.32:19559/fileServices/resources/test_images/image/20200512/1589256092820.png");
        System.out.println(aBoolean);

結果:
f517a648a4cc2b9c13f22c6d2a374668
02d39852d0f73056b4822906ff22bc1b
false

第二種比較文件內容

   /**
     * 判斷兩個文件的內容是否相同,文件名要用絕對路徑
     *
     * @param filePath1 :文件1的絕對路徑
     * @param filePath2 :文件2的絕對路徑
     * @return 相同返回true,不相同返回false
     */
    public static boolean isSameFile(String filePath1, String filePath2) {
        InputStream fis1 = null;
        InputStream fis2 = null;
        try {
            URL url = new URL(filePath1);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            fis1 = conn.getInputStream();

            URL url1 = new URL(filePath2);
            HttpURLConnection conn1 = (HttpURLConnection) url.openConnection();
            fis2 = conn1.getInputStream();

            int len1 = fis1.available();//返回總的字節數
            int len2 = fis2.available();

            if (len1 == len2) {//長度相同,則比較具體內容
                //建立兩個字節緩衝區
                byte[] data1 = new byte[len1];
                byte[] data2 = new byte[len2];

                //分別將兩個文件的內容讀入緩衝區
                fis1.read(data1);
                fis2.read(data2);

                //依次比較文件中的每一個字節
                for (int i = 0; i < len1; i++) {
                    //只要有一個字節不同,兩個文件就不一樣
                    if (data1[i] != data2[i]) {
                        System.out.println("文件內容不一樣");
                        return false;
                    }
                }
                System.out.println("兩個文件完全相同");
                return true;
            } else {
                //長度不一樣,文件肯定不同
                return false;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {//關閉文件流,防止內存泄漏
            if (fis1 != null) {
                try {
                    fis1.close();
                } catch (IOException e) {
                    //忽略
                    e.printStackTrace();
                }
            }
            if (fis2 != null) {
                try {
                    fis2.close();
                } catch (IOException e) {
                    //忽略
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

測試運行

        System.out.println(isSameFile("http://117.71.53.32:19559/fileServices/resources/test_images/image/20200512/1589262909067.png", "http://117.71.53.32:19559/fileServices/resources/test_images/image/20200512/1589256092820.png"));

兩個文件完全相同
true

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章