Commons Upload Progress

1086759363000

the JSP edition of megaupload has been in the back burner for many moons. Recently I had a happy encounter with the Commons File Upload handler and felt like building a progress bar for it.

The JSP edition at the moment does include progress bars for both jspupload and http://jakarta.apache.org"struts, but there is a lot of room for improvement. On the other hand the commons uploader is pretty neat and adding the the progress bar turned out to be quite easy. Best of all it doesn't look or feel like an ugly hack (like the others)

The incoming mutlipart/form-data is handled by the MultipartInputStream calss, (The readBodyData() method to be precise). A few lines are added to the source code to write out the number of bytes read to a temporary file. Next the JSP that actually displays the progress uploaded will inspect the contents of this file to calculate the percentage uploaded.

The filename has to be unique and the progress.jsp page needs to know what this unique name is. If we build up the name using the session identifier both goals can be achieved. However the MultipartStream class does not make use of the HttpServletRequest object associated with the ongoing upload, but that's easily overcome by simply adding a request property to the class.

Two different files will actually be used, the first one wil be written to once and it will only have the total length in bytes of the incoming data. The progress.jsp page is supposed to read this and delete it. The second file will contain the actual number of bytes read.

Two files are better than one because we if we attempted to write them to one file we will have a better chance of encountering a lock. The idea is to minimize locks. In the PHP edition we had to worry about the locking ourselves, thankfully this is java.

comments powered by Disqus