Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get real file extension -Java code

Tags:

java

I would like to determine real file extension for security reason.

How can I do that?

like image 717
Chan Pye Avatar asked Jan 19 '10 04:01

Chan Pye


People also ask

How do I find the unknown extension of a file?

Search the FILExt database to find out how to open the unknown file. Searching online is always the best method to learn about a new file type, and the best place to do it is the Filext database. It is an impressive database which contains over 50,000 known file extensions.

How do I create a .file extension?

A file extension is just the portion of the file name after the last period. The file extension is . txt which typically indicates that the file contains text data. To create your own file extension all you need to do is to place the desired extension after the last period in the filename.

How do I show file extensions in CMD?

Here is how you can use it so Windows will display file extensions: Press Win + R to open a Run dialog. Type cmd and click OK. In the Command Prompt window copy the next command reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced /v HideFileExt /t REG_DWORD /d 0 /f.


1 Answers

Supposing you really mean to get the true content type of a file (ie it's MIME type) you should refer to this excellent answer.

You can get the true content type of a file in Java using the following code:

File file = new File("filename.asgdsag");
InputStream is = new BufferedInputStream(new FileInputStream(file));
String mimeType = URLConnection.guessContentTypeFromStream(is);
like image 59
Mark Elliot Avatar answered Oct 07 '22 04:10

Mark Elliot