1

I am doing file uploads in tomcat 7 using the servlet 3 api.
I'm setting the <multipart-config> in web.xml.

How can I get access to the value of max-file-size in my java code, so I can tell the user what the max file size is?

I've trawled through the apis for Servlet, ServletContext, ServletConfig, ServletRegistration and can't find anything. The multipart-config values are not among the initParameters.

I think another way of asking this is, how can I get hold of the MultipartConfigElement object for the servlet? Again, having trawled through the api's, I can't find any way of retrieving this.

cmgharris
  • 389
  • 4
  • 11

1 Answers1

2

I have found a way of doing what I want, though it uses reflection to get at private, undocumented parts of the servlet config, and so is far from ideal - no guarantee it would continue to work on a tomcat upgrade. But within the servlet, the following will get the MultipartConfigElement (which has methods to return the various bits of the configuration):

ServletConfig scfig = getServletConfig();
MultipartConfigElement mce = null;
try {
    Field config = scfig.getClass().getDeclaredField("config");
    config.setAccessible(true);
    mce = ((StandardWrapper)config.get(scfig)).getMultipartConfigElement();
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException  e) {
    e.printStackTrace();
}
cmgharris
  • 389
  • 4
  • 11