Arunprasad
Arunprasad

Reputation: 577

How to get file name and file Type using inputStream?

guys i'm using inputStream for file Download . now i want to pass fileName and fileType into DefaultStreamedContent .now how can i find fileName and FileType using inputStream .

InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
fileDownload = new DefaultStreamedContent(inputStream,**fileType,fileName**);

Upvotes: 7

Views: 47904

Answers (1)

BalusC
BalusC

Reputation: 1109132

This information can't be extracted from InputStream. This information can only be extracted based on the filePath (and with little help of java.io.File to easily get the filename).

File file = new File(filePath);
InputStream inputStream = new FileInputStream(file);
String fileName = file.getName();
String fileType = FacesContext.getCurrentInstance().getEexternalContext().getMimeType(fileName);
fileDownload = new DefaultStreamedContent(inputStream, fileType, fileName);

The ExternalContext#getMimeType() is determined based on <mime-mapping> entries in web.xml. The servletcontainer has already a whole bunch definied by itself (in Tomcat, check /conf/web.xml) but you can extend and override it by (re)defining them in webapp's own /WEB-INF/web.xml like as follows for an XLSX type:

<mime-mapping>
    <extension>xlsx</extension>
    <mime-type>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</mime-type>
</mime-mapping>

Upvotes: 6

Related Questions