<p>I have a servlet that is used for many different actions, used in the <a href="http://java.sun.com/blueprints/patterns/FrontController.html" rel="noreferrer">Front Controller pattern</a>.  Does anyone know if it is possible to tell if the data posted back to it is enctype="multipart/form-data"?  I can't read the request parameters until I decide this, so I can't dispatch the request to the proper controller.</p>

<p>Any ideas?</p>

[{'answer_id': 66481, 'author': 'Loren Segal', 'author_id': 6436, 'author_profile': 'https://Stackoverflow.com/users/6436', 'pm_score': 5, 'selected': True, 'text': '<p>Yes, the <code>Content-type</code> header in the user agent\'s request should include <code>multipart/form-data</code> as described in (at least) the HTML4 spec:</p>\n\n<p><a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2" rel="noreferrer">http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2</a></p>\n'}, {'answer_id': 66509, 'author': 'Kyle Boon', 'author_id': 1486, 'author_profile': 'https://Stackoverflow.com/users/1486', 'pm_score': 3, 'selected': False, 'text': '<p>You can call a method to get the content type.</p>\n\n<p><a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html#getContentType()" rel="nofollow noreferrer">http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html#getContentType()</a></p>\n\n<p>According to <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2" rel="nofollow noreferrer">http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2</a>, the content type will be "multipart/form-data".</p>\n\n<p>Don\'t forget that:</p>\n\n<ol>\n<li><p>request.getContentType() may return null.</p></li>\n<li><p>request.getContentType() may not be equal to "multipart/form-data", but may just start with it.</p></li>\n</ol>\n\n<p>So, with all this in mind:</p>\n\n<pre><code>if (request.getContentType() != null &amp;&amp; \n    request.getContentType().toLowerCase().indexOf("multipart/form-data") &gt; -1 ) \n{\n    &lt;&lt; code block &gt;&gt;\n} \n</code></pre>\n'}, {'answer_id': 66534, 'author': 'enricopulatzo', 'author_id': 9883, 'author_profile': 'https://Stackoverflow.com/users/9883', 'pm_score': 1, 'selected': False, 'text': "<p>You'll have to read the request parameters in order to determine this, at least on <em>some</em> level.  The ServletRequest class has a getContentType method that you'll want to look at.</p>\n"}, {'answer_id': 579519, 'author': 'Darren Hicks', 'author_id': 59075, 'author_profile': 'https://Stackoverflow.com/users/59075', 'pm_score': 4, 'selected': False, 'text': '<p>If you are going to try using the request.getContentType() method presented above, be aware that:</p>\n\n<ol>\n<li>request.getContentType() may return null.</li>\n<li>request.getContentType() may not be <em>equal</em> to "multipart/form-data", but may just start with it.</li>\n</ol>\n\n<p>With this in mind, the check you should run is :</p>\n\n<pre><code>if (request.getContentType() != null &amp;&amp; request.getContentType().toLowerCase().indexOf("multipart/form-data") &gt; -1 ) {\n// Multipart logic here\n}\n</code></pre>\n'}, {'answer_id': 7011086, 'author': 'awm129', 'author_id': 332793, 'author_profile': 'https://Stackoverflow.com/users/332793', 'pm_score': 2, 'selected': False, 'text': '<p>ServletFileUpload implements isMultipartContent().  Perhaps you can lift this implementation (as opposed to going through the overhead to create a ServletFileUpload) for your needs.</p>\n\n<p><a href="http://www.docjar.com/html/api/org/apache/commons/fileupload/servlet/ServletFileUpload.java.html" rel="nofollow">http://www.docjar.com/html/api/org/apache/commons/fileupload/servlet/ServletFileUpload.java.html</a></p>\n'}, {'answer_id': 54141363, 'author': 'SiuFay', 'author_id': 6647618, 'author_profile': 'https://Stackoverflow.com/users/6647618', 'pm_score': 0, 'selected': False, 'text': '<p><a href="https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getParts()" rel="nofollow noreferrer">https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getParts()</a></p>\n\n<p>java.util.Collection getParts()</p>\n\n<p>Throws:\nServletException - if this request is not of type multipart/form-data</p>\n'}, {'answer_id': 56460683, 'author': 'zb226', 'author_id': 1529709, 'author_profile': 'https://Stackoverflow.com/users/1529709', 'pm_score': 1, 'selected': False, 'text': '<p>To expand on <a href="https://stackoverflow.com/a/7011086/1529709">awm129\'s answer</a> - Apache commons\' implementation corresponds to this:</p>\n\n<pre><code>if (request != null \n        &amp;&amp; request.getContentType() != null \n        &amp;&amp; request.getContentType().toLowerCase(Locale.ENGLISH).startsWith("multipart/")) {\n    ...\n}\n</code></pre>\n\n<p>You can write it a lot shorter using Apache commons\' <code>org.apache.commons.lang3.StringUtils</code>:</p>\n\n<pre><code>if (StringUtils.startsWithIgnoreCase(request.getContentType(), "multipart/")) { \n    ... \n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/66423', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4257/']