简体   繁体   English

如何获取扩展名错误的上传文件的正确文件类型?

[英]How to get the correct file type of uploaded file which is with wrong extension?

eg.例如。 We can change a.txt file name as a.doc.我们可以将 a.txt 文件名更改为 a.doc。 But still the file type is 'Text Document', it is not converted into 'Doc' file.但是文件类型仍然是“文本文档”,它不会转换为“文档”文件。 So, I want coding to recognize the type of such file.. In this case, 'a.doc' file should return the type of file as 'Text Document'.所以,我想通过编码来识别此类文件的类型。在这种情况下,“a.doc”文件应将文件类型返回为“文本文档”。 Not as 'Doc'.不像'Doc'。

There is but one way as far as I know: you'd have to recognize the type of file by its content. 据我所知,只有一种方法:您必须通过文件的内容来识别文件的类型。

A generic way to deal with that is to not look at the whole file but only the first X bytes (which will generally be the file header) and try to find a recognizable marker in there. 解决该问题的一种通用方法是不查看整个文件,而是仅查看前X个字节(通常是文件头),然后尝试在其中查找可识别的标记。 Many file types start with an identifying byte sequence for example. 例如,许多文件类型都以标识字节序列开头。

Is this you are searching . 这是您要搜索的。 Please check this url 请检查网址

http://www.rgagnon.com/javadetails/java-0487.html

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

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