简体   繁体   English

如何在服务器端验证上传的文件是否为mp3?

[英]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. 我在我的应用程序中上传mp3文件,我已经完成了上传文件,它工作正常。 I want to validate the file in server side, checking if the uploaded file is in a correct format(.mp3) or not. 我想在服务器端验证文件,检查上传的文件是否格式正确(.mp3)。 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. 我正在使用java servlet进行上传,我有预处理器在上传之前进行验证。

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. 除了扩展,您可以深入了解MP3规范 ,读取文件并验证它是否符合定义。

Probably you can use some Java library that already interprets the spec, like this one 可能你可以使用一些已经解释规范的Java库,就像这个一样

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 "." 我不是java专家,但是如果你需要检查文件是否有“mp3”扩展名,为什么不尝试用“。”分割文件的名称。 character, and then check if the last part is "mp3"? 字符,然后检查最后一部分是否是“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. 如果你真的想检查它是否正确到达,你必须安装一个MP3编码器/解码器并通过它来拉取文件。 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). 如果你只是想验证文件是否确实是一个mp3文件(但不是它是否已损坏),那么检查文件头就足够了,文件头的预期内容可以在文件格式定义中找到,该文件格式定义可用于在很多地方网上免费(其中一些可能已经过时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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