如何识别java中的文件是否已损坏

时间:2021-09-13 02:38:53

I have a requirement to upload the documents. In this I dont want to upload corrupted files in to server. Can some one please tell me how I can identify corrupted file in java

我需要上传文件。在此我不想将损坏的文件上传到服务器。有人可以告诉我如何在java中识别损坏的文件

1 个解决方案

#1


4  

You could get a MD5 or other checksum of the file and compare the values created on the client and server after the transfer though this would only tell you if the file was corrupted during transport and not that the original file is actually valid/correct for the intended use/type.

您可以获取文件的MD5或其他校验和,并比较传输后在客户端和服务器上创建的值,但这只会告诉您文件在传输过程中是否已损坏,而不是原始文件实际上是有效/正确的预期用途/类型。

Files are just containers for bytes with some filesystem descriptors. How they are read and used by applications is specific to each and every file format. If you wanted to validate the contents of files prior to upload as actually valid for use by some application or another you will either need to implement (or find) code to evaluate every file you want to support in a different way according to the requirements for that type or perhaps try to read/load/use the file in some sand-boxed application instance beforehand (sand-boxed as invalid files may break things if really invalid or potentially malicious).

文件只是具有一些文件系统描述符的字节容器。应用程序如何读取和使用它们特定于每种文件格式。如果您想在上传之前验证文件的内容实际上有效供某些应用程序或其他应用程序使用,您将需要实现(或查找)代码,以根据要求以不同的方式评估您要支持的每个文件。该类型或者可能尝试在某个沙盒应用程序实例中预先读取/加载/使用该文件(如果真的无效或可能是恶意的话,将无效文件打包为无效文件可能会破坏)。

#1


4  

You could get a MD5 or other checksum of the file and compare the values created on the client and server after the transfer though this would only tell you if the file was corrupted during transport and not that the original file is actually valid/correct for the intended use/type.

您可以获取文件的MD5或其他校验和,并比较传输后在客户端和服务器上创建的值,但这只会告诉您文件在传输过程中是否已损坏,而不是原始文件实际上是有效/正确的预期用途/类型。

Files are just containers for bytes with some filesystem descriptors. How they are read and used by applications is specific to each and every file format. If you wanted to validate the contents of files prior to upload as actually valid for use by some application or another you will either need to implement (or find) code to evaluate every file you want to support in a different way according to the requirements for that type or perhaps try to read/load/use the file in some sand-boxed application instance beforehand (sand-boxed as invalid files may break things if really invalid or potentially malicious).

文件只是具有一些文件系统描述符的字节容器。应用程序如何读取和使用它们特定于每种文件格式。如果您想在上传之前验证文件的内容实际上有效供某些应用程序或其他应用程序使用,您将需要实现(或查找)代码,以根据要求以不同的方式评估您要支持的每个文件。该类型或者可能尝试在某个沙盒应用程序实例中预先读取/加载/使用该文件(如果真的无效或可能是恶意的话,将无效文件打包为无效文件可能会破坏)。