How to check if xlsx file is password protected or not using apache poi

前端 未结 3 620
广开言路
广开言路 2020-12-07 03:42

How to check if xlsx file is password protected or not. we can check for xls file as follows

FileInputStream fin = new FileInputStream(new File(\"C:/Book1.xl         


        
3条回答
  •  醉话见心
    2020-12-07 04:05

    First,

    public boolean isEncrypted(String path) {
    
        try {
            try {
                new POIFSFileSystem(new FileInputStream(path));
            } catch (IOException ex) {
    
            }
            System.out.println("protected");
            return true;
        } catch (OfficeXmlFileException e) {
            System.out.println("not protected");
            return false;
        }
    }
    

    then,

    if (isEncrypted(sourcepath)) {
            org.apache.poi.hssf.record.crypto.Biff8EncryptionKey.setCurrentUserPassword("1234");
            POIFSFileSystem filesystem = new POIFSFileSystem(new FileInputStream(inpFn));
            EncryptionInfo info = new EncryptionInfo(filesystem);
            Decryptor d = Decryptor.getInstance(info);
    
            if (!d.verifyPassword("1234")) {
                System.out.println("Not good");
            } else {
                System.out.println("Good!");
            }
    
            in = d.getDataStream(filesystem);
        } else {
            in = new FileInputStream(inpFn);
        }
        try {
            XSSFWorkbook wbIn = new XSSFWorkbook(in);
    .
    .
    .
    

提交回复
热议问题