Java校验文件是否损坏


Java校验文件是否损坏

经常在程序操作文件时,遇到文件以及损坏的问题,那么如何校验文件是否损坏呢?这就需要Apache Tika包了,maven引用如下:

<!-- https://mvnrepository.com/artifact/org.apache.tika/tika-parsers -->
<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-parsers</artifactId>
    <version>1.16</version>
</dependency>
 
<!-- https://mvnrepository.com/artifact/org.apache.tika/tika -->
<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika</artifactId>
    <version>1.16</version>
    <type>pom</type>
</dependency>
 
<!-- https://mvnrepository.com/artifact/org.apache.tika/tika-core -->
<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-core</artifactId>
    <version>1.16</version>
</dependency>

使用方法:

try {
     Tika tika = new Tika();
     URL url = new URL(urlStrng);
     File file = new File("");
     InputStream inputStream = new FileInputStream(file);
     tika.parseToString(inputStream);
     tika.parseToString(file);
     tika.parseToString(url);
     //System.out.println(sb);
     return true;
 } catch (TikaException e) {
     e.printStackTrace();
     log.info(e.toString());
     return false;
 } catch (Exception e) {
     e.printStackTrace();
     log.error("合同号:{}", contranctId);
     return false;
 }

如果校验损坏则会返回false

校验成功则为true。
————————————————
版权声明:本文为CSDN博主「哈利路亚--Java」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/a491857321/java/article/details/77838902


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM