Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8966749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:05:21+00:00 2026-06-15T17:05:21+00:00

I have made use of Apache MyFaces Tomahawk to utilize file upload in a

  • 0

I have made use of Apache MyFaces Tomahawk to utilize file upload in a JSF project. I have successfully retrieve the uploaded file, however every time when I try to identify the file type, particularly for zip file, the getContentType() function always return application/octet-stream. Why is that so?

I suppose it is a config error in my web.xml, the following is the file:

<?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>

Could anyone give me a hand?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:05:22+00:00Added an answer on June 15, 2026 at 5:05 pm

    The outcome of Tomahawk’s UploadedFile#getContentType() method is not been set by the server based on web.xml mime mapping, but by the client itself straight in the multipart/form-data header before sending it to the server. It’s thus merely extracted from the request body.

    If it does not return the value you expect or desire, then you could always check the mime type based on the file extension of the uploaded file by ExternalContext#getMimeType().

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

    Keep however in mind that the file extension can be faked by the client (as could be the content type in the multipart/form-data header!), so detection based on extension (as well as the retrieved/determined content type!) is not necessarily robust enough. The client might for instance have renamed foo.exe to foo.zip or so. The best way to determine if the file is really a ZIP file is to just open the file as a ZIP file and catch any exception thrown. The standard Java SE API offers the ZipFile constructor for this.

    Assuming that you’re storing uploaded files like follows (FilenameUtils and IOUtils are from Apache Commons IO, which you should already have as it’s one of the required dependencies of 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);
    }
    

    Then you should be able to determine if it’s a valid ZIP file as follows:

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

    See also:

    • When do browsers send application/octet-stream as Content-Type?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want use spring security taglibs in my jsf project,so I've made ​​the following
I have made a registration program. Making use of mysql database. Can I still
So I have made a .dll containing some methods that I wish to use
I am trying to use file_get_contents.I have made sure that allow_url_fopen is enabled in
I use Acceleo in order to generate code with a model I have made.
I have finally made a production worthy app with play, and use the play
I have an application in which we use a hand-made build system.The reason for
I'm trying to use a .htaccess file on the Apache Server running on my
I have a very simple file upload PHP script. I am testing with a
I've made an application using CodeIgniter, and I tried to use the apache mod_rewrite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.