Get the real file extension – Java code

For security reasons, I want to determine the real file extension

How can I do this?

Solution

If you really want to get the real content type of the file (that is, its MIME type), you should refer to this excellent answer

You can use the following code to get the real content type of a file in Java:

File file = new File("filename.asgdsag");
InputStream is = new BufferedInputStream(new FileInputStream(file));
String mimeType = URLConnection.guessContentTypeFromStream(is);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>