【问题标题】:How to get file name and file Type using inputStream?如何使用 inputStream 获取文件名和文件类型?
【发布时间】:2012-10-25 04:40:24
【问题描述】:

伙计们,我正在使用 inputStream 进行文件下载。现在我想将 fileName 和 fileType 传递给 DefaultStreamedContent 。现在我如何使用 inputStream 找到 fileName 和 FileType 。

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

【问题讨论】:

  • 你为什么不用fileDownload = new DefaultStreamedContent(inputStream);

标签: spring jsf primefaces


【解决方案1】:

无法从InputStream 中提取此信息。这些信息只能基于filePath 提取(并在java.io.File 的帮助下轻松获取文件名)。

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);

ExternalContext#getMimeType() 是根据web.xml 中的<mime-mapping> 条目确定的。 servletcontainer 已经自己定义了一大堆(在 Tomcat 中,检查 /conf/web.xml),但您可以通过在 webapp 自己的 /WEB-INF/web.xml 中(重新)定义它们来扩展和覆盖它,如下所示用于 XLSX 类型:

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

【讨论】:

    猜你喜欢
    • 2012-03-24
    • 2013-05-28
    • 2019-02-06
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    相关资源
    最近更新 更多