【问题标题】:Tomahawk UploadedFile#getContentType() returns application/octet-stream for ZIP filesTomahawk UploadedFile#getContentType() 为 ZIP 文件返回 application/octet-stream
【发布时间】:2012-11-25 11:22:45
【问题描述】:

我使用 Apache MyFaces Tomahawk 在 JSF 项目中利用文件上传。我已经成功检索了上传的文件,但是每次我尝试识别文件类型时,特别是对于 zip 文件,getContentType() 函数总是返回application/octet-stream。为什么会这样?

我想这是我的web.xml 中的配置错误,以下是文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <display-name>Project 1</display-name>

    <context-param>
        <param-name>facelets.LIBRARIES</param-name>
        <param-value>/WEB-INF/el-taglib.xml</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.CHECK_EXTENSIONS_FILTER</param-name>
        <param-value>true</param-value>
    </context-param>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>MyFacesExtensionsFilter</filter-name>
        <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>MyFacesExtensionsFilter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

    <mime-mapping>
        <extension>zip</extension>
        <mime-type>application/zip</mime-type>
    </mime-mapping>

</web-app>

谁能帮帮我?

【问题讨论】:

    标签: java file-upload jsf-2 zip tomahawk


    【解决方案1】:

    Tomahawk 的UploadedFile#getContentType() 方法的结果不是由服务器根据web.xml mime 映射设置的,而是由客户端本身在发送到服务器之前直接在multipart/form-data 标头中设置的。因此,它只是从请求正文中提取出来的。

    如果它没有返回您期望或期望的值,那么您可以随时根据ExternalContext#getMimeType()上传文件的文件扩展名检查mime类型。

    String contentType = externalContext.getMimeType(uploadedFile.getName());
    

    但请记住,文件扩展名可以被客户端伪造(可能是 multipart/form-data 标头中的内容类型!),因此基于扩展名的检测(以及检索/确定的内容类型!)不一定足够健壮。例如,客户端可能已将foo.exe 重命名为foo.zip 左右。确定文件是否真的是 ZIP 文件的最佳方法是将文件作为 ZIP 文件打开并捕获任何引发的异常。标准 Java SE API 为此提供了 ZipFile 构造函数。

    假设您正在存储上传的文件,如下所示(FilenameUtilsIOUtils 来自 Apache Commons IO,您应该已经拥有它,因为它是 Tomahawk 所需的依赖项之一):

    String prefix = FilenameUtils.getBaseName(uploadedFile.getName()); 
    String suffix = FilenameUtils.getExtension(uploadedFile.getName());
    File uploadLocation = new File("/path/to/uploads"); // Make it configureable!
    File file = File.createTempFile(prefix + "-", "." + suffix, uploadLocation);
    
    InputStream input = uploadedFile.getInputStream();
    OutputStream output = new FileOutputStream(file);
    
    try {
        IOUtils.copy(input, output);
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(input);
    }
    

    那么您应该能够确定它是否是一个有效的 ZIP 文件,如下所示:

    try {
        new ZipFile(file);
    } catch (ZipException e) {
        // Here, we know that it's not a valid ZIP file!
    }
    

    另见:

    【讨论】:

      猜你喜欢
      • 2016-03-13
      • 2014-06-08
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2010-12-28
      相关资源
      最近更新 更多