Tuesday, July 7, 2009

File Upload Component in NetBeans 6.5

My next portlet has a requirement to provide the ability for a user to browse their local filesystem and select a file to be uploaded to the server. Support for this in HTML forms has been around for awhile by using a form attribute enctype="multipart/form-data" and attribute type="file" in the form input element. The HTML form is the easy part to understand. The hard part is knowing what to do on the server-side when the form is submitted.

Since I have been using NetBeans 6.5, I decided to try to find an example of how to do this in a NetBeans project. Searches led me to a NetBeans tutorial "Uploading Files in NetBeans Visual Web JSF Application." I'm not familiar with Visual Web JSF but a step-by-step tutorial looked like something I could follow. And if I had the code in a NetBeans Web Application project then it should be easy enough to take the pieces I needed and put them in a portlet project.

I made sure that I had all the software and resources that were listed as requirements. I looked through the NetBeans plugins and installed those related to JSF and Visual Web JSF. Even though the tutorial didn't say anything about plugins it seemed logical that those would be needed. A couple of missing references went away when I installed the plugins after an initial attempt to make a project with the Visual Web JavaServer Faces framework. Unfortunately, I never got any further.

I fiddled around for a day trying different plugins and looking on the web and posting to the NetBeans forums. But I'm stuck with a Reference problem--"jsf12-support library could not be found". I also noticed that all of the default source files that get created are unable to import anything from com.sun.rave.web.ui.appbase. This is in spite of the fact that the IDE shows that the libraries are there in the appbase.jar.

Enough of messing around with the NetBeans framework. Back to the web to look around for other examples. Surely there has to be a way to do this that doesn't require JSF. After all, it looks to me like the JSF is only a convenience for creating buttons and form fields on the HTML part of the application. It doesn't seem tied to the back-end server processing.

Finally, at http://www.scribd.com/doc/220346/How-to-upload-files-using-JavaServer-Pages I found something that looked simple enough. I needed a few tweaks to get it to work within the portlet environment but, lo and behold, I can now upload files. And with only about 30 lines of code in the processAction method of the portlet class. Thanks so much to whomever posted the Scribd document.