java 甄别 pdf文件是否被二次修改

package com.company;
import com.itextpdf.text.pdf.PdfReader;
import java.io.*;


public class Main {
    public static void main(String[] args) {
        Thread t5 =new Thread(){
            public void run(){
                while(true){
                    try{
                        sleep(1000);
                        new File("请放进来").mkdirs();
                        File[] files = new File("请放进来").listFiles();
                        if(files!=null&&files.length>0){
                            for(File file1:files){
                                if(file1.getName().toLowerCase().indexOf(".pdf")!=-1){
                                    System.out.println(file1.getName());
                                    String parent = file1.getParent();
                                    if(!isTrue(file1)){
                                        reName(file1,new File(parent+"/错误/"+file1.getName()));
                                    }else{
                                        reName(file1,new File(parent+"/正确/"+file1.getName()));
                                    }
                                }
                            }
                        }

                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
        };
        t5.start();
    }

    private static void reName(File before, File after) {
        String parent = after.getParent();
        if(!new File(parent).exists()){
            new File(parent).mkdirs();
        }
        before.renameTo(after);
    }

    public static boolean isTrue(File file){
        try{
            PdfReader reader = new PdfReader(file.toString());
            byte[] metadata = reader.getMetadata();
            String s = new String(metadata);
            String CreateDate = s.substring(s.indexOf("<xmp:CreateDate>")+"<xmp:CreateDate>".length(),s.indexOf("</xmp:CreateDate>"));
            String ModifyDate = s.substring(s.indexOf("<xmp:ModifyDate>")+"<xmp:ModifyDate>".length(),s.indexOf("</xmp:ModifyDate>"));
            reader.close();
            return CreateDate.substring(0,"2021-04-14T16:23".length()).equals(ModifyDate.substring(0,"2021-04-14T16:23".length()));
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;

    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值