Reputation: 181
I have a form:
<form action='?hasScenario=1' method='post' enctype='multipart/form-data'>
<input type='file' name='file'/>
<input type='submit' />
</form>
In tomcat 8.0 I want to do:
private void handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
Part filePart = request.getPart("file");
....
}
In documentation I see I should get null if user doesn't enter any value. I havn't (intentionally) configured anything special for multipart/file uploading in web.xml or server. But instead of null I get:
java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided
I would like to handle nicely cases, when some parameters are not provided, how to do that? Catching and doing nothing on IllegalStateException is not nice way for me - in case of no parameter I'd like to ask user for file instead of scaring him with 'error/warning' words.
Upvotes: 1
Views: 851
Reputation: 181
It is a great way to find answer myself - just ask question to others :)
multipart-config in web.xml is a must. So my servlet part looks currently like:
<servlet>
<description>Scenario</description>
<servlet-name>Scenario</servlet-name>
<servlet-class>path-to-the-servlet</servlet-class>
<multipart-config>
<max-file-size>3145728</max-file-size>
<max-request-size>5242880</max-request-size>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>Scenario</servlet-name>
<url-pattern>/scenario</url-pattern>
</servlet-mapping>
Before calling getPart it's important to check there are any data, like with:
if (request.getContentType() != null)
Part filePart = request.getPart("file");
...
And so finally filePart is null or a valid variable
Still I can't understand how have they made getParameter working in this post How to upload files to server using JSP/Servlet? but this is different story :)
Upvotes: 1