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
-------------已经触及底线 感谢您的阅读-------------