You could try opening the InputStream using ZipInputStream - if it fails or you trap an exception then you know it's probably not an xlsx (or at least a corrupt one). If it does open then you could also be more through and check for the presence of certain xml files and/or folders in the zip to confirm that it is an xlsx and not some other OpenDocument or OpenXML (docx/pptx) file.
Not sure how to confirm xls, presumably the format has some identifier bytes in the header you could look for? Or simply try opening the file with the HSSF usermodel if the above has failed/dropped through (may be prohibitively expensive time/memory wise depending on what it is you're actually doing). Probably not a lot of help... >________________________________ > From: Eric Peters <[email protected]> >To: [email protected] >Sent: Monday, July 15, 2013 5:01 PM >Subject: Sniffing File Type from InpuStream? > > >If you have an InputStream, what's a good best practice for determining >whether it's a HSSF vs XSSF file? I'm assuming by somehow wrapping the >InputStream in a BufferedInputStream and marking/resetting before actually >doing the processing. > >But other than just looking at file extension, anyone have an idea how I >can unify my Excel File Reader to just properly handle the file type. > >Thanks, > >Eric > > >
