简体   繁体   中英

How to validate the uploaded file is mp3 or not in server side?

I am uploading the mp3 file in my application, I have finished uploading the file and it works fine. I want to validate the file in server side, checking if the uploaded file is in a correct format(.mp3) or not. Client side validation works fine but I want to check it in server side as well, because there are changes to the upload the file by simply changing the extension.

I am using java servlet for uploading and I am having preprocessor to do validation before uploading.

Anyone help me, Thanks in advance.

Apart from the extension, you can dive into the MP3 specification , read the file and verify if it conforms to the definition.

Probably you can use some Java library that already interprets the spec, like this one

But, unless you really need to, I think the extension checking is sufficient.

I'm not a java expert, but if you need to check if the file has "mp3" extension, why don't you try to split the name of the file by "." character, and then check if the last part is "mp3"?

you'd have to install an MP3 encoder/decoder and pull the file through that if you really want to check whether it arrived correctly. If you just want to verify whether the file is indeed an mp3 file (but not whether it is corrupt or not) it suffices to check the file header, the expected content of which can be found in the file format definition, which is available for free on the web in many places (some of which may be outdated).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM