Apache Tika is a library for file type detection and content extraction from files of various formats. When uploading files to a server and parsing them, you often need to determine whether the files are damaged. We can use tika to detect whether the file is damaged Maven is introduced as follows: <dependency> <groupId>org.apache.tika</groupId> <artifactId>tika-app</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.11.0</version> </dependency> If there is a conflict in the jar packages, you can introduce them as follows: <dependency> <groupId>org.apache.tika</groupId> <artifactId>tika-core</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>org.apache.tika</groupId> <artifactId>tika-parsers</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.11.0</version> </dependency> Use tika to detect whether the file is damaged: If reading from the input stream fails, the parse method throws an IOException. If the document obtained from the stream cannot be parsed, a TikaException is thrown. If the processor cannot handle the event, a SAXException is thrown. When a document cannot be parsed, it indicates that the document is corrupted. Execution process: public static void main(String[] args) { try { //Assume sample.txt is in your current directory File file = new File("D:\\Test.txt"); boolean result = isParseFile(file); } catch (Exception e) { e.printStackTrace(); } } /** * Verify if the file is corrupted* * @param file file * @return true/false * @throws Exception */ private static boolean isParseFile(File file) throws Exception { try { Tika tika = new Tika(); String filecontent = tika.parseToString(file); System.out.println(filecontent); return true; } catch (TikaException e) { return false; } } Output:
Summarize The above is the method of Apache Tika to detect whether the file is damaged. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: Ant designing vue table to achieve a complete example of scalable columns
>>: Mysql 8.0 installation and password reset issues
1.0 Redis persistence Redis is an in-memory datab...
What is JConsole JConsole was introduced in Java ...
When talking about this issue, some people may ask...
Table of contents 1. Introduction 2. Main text 2....
Or write down the installation process yourself! ...
1. Form <form id="" name=""...
Overflow Hide It means hiding text or image infor...
Table of contents 1. Ternary operator judgment 2....
We know that MySQL is a persistent storage, store...
Through permission-based email marketing, not onl...
Table of contents Preface Motivation for Fragment...
I believe everyone has played scratch tickets. Wh...
In the previous blog, we talked about using Nginx...
The excellence of Linux lies in its multi-user, m...
Let’s look at the effect first: This effect looks...