0

I have a file with extension xlsx, now if i change its extension to png, then is there any way to get the actual content type of that file i.e. xlsx. I have to determine it for some security reasons. have to restrict the files other than image files. But user can upload the files after renaming them as i am checking files by extensions. Please help.

I did this:----- final MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();

    System.out.println( fileTypeMap.getContentType("/home/ist-48/Downloads/report.png"));

actually report is a xlsx file.. but its returning:--- image/png

vibhac
  • 13
  • 3
  • It would be better if you could elaborate a bit more citing some examples. – Blip May 19 '15 at 11:00
  • public static void main(String ar[]) { final MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap(); System.out.println( fileTypeMap.getContentType("/home/task.png")); } – vibhac May 19 '15 at 11:11
  • this gives me image/png but its actually an xlsx file which i have renamed to png – vibhac May 19 '15 at 11:12

2 Answers2

0

You will find same in below library

http://tika.apache.org/0.8/detection.html

Krutik Jayswal
  • 3,165
  • 1
  • 15
  • 38
0

Yes, there's a new set of interfaces provided by POI that work with both types.

Use the WorkbookFactory.create() method to get a Workbook: http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/WorkbookFactory.html

You can check for excel files without relying on file extensions (which are unreliable - many csv files have xls extensions for example but cannot be parsed by POI) using the following:

//simple way to check for both types of excel files
public boolean isExcel(InputStream i) throws IOException{
    return (POIFSFileSystem.hasPOIFSHeader(i) || POIXMLDocument.hasOOXMLHeader(i));
}
Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154