Menu

Apache Commons File Upload – Order Dependency

I have a web page that I’m not responsible for. It has a mixture of inputs – mainly text and file inputs. I also have a Java Servlet that this web page calls, which I didn’t write originally but now look like I’m responsible for maintaining.

Unfortunately the file processing part of the servlet requires that that some of the other parameters have already been passed across – mainly to create the temporary directory to upload the files to. The servlet however just takes the fields as they come, and then blindy does the upload whether or not the required parameters have been set.

During testing its been noticed that these ‘null’ directories are being created. The initial solution was to re-order the original web page to have the parameters come across in the correct order, which is a fine solution, but of course without taking responsibility for that webpage I’m not in a position to guarantee they’ll remain that way. So a better solution needed to be found.

An Example of the Existing Code (with error condition)

A simple jsp page, contain 2 text inputs and a file input. Note the second input comes after the file input. I’m pretending in this example to require the 2nd text input as part of the directory location.

Again – the solution isn’t meant as production code. I needs quite a bit of tidying up and refactoring, but I think the essence demonstrates the prolbem and its solution.

It is tempting to finish processing the form, adding all the files into the uploadFile list and processing them at the end. However, but optionally adding them to the list, we can at least process them in the hope the original web page hasn’t been altered.