I am using Apache Commons File Uploader API. It is working successfully when I use absolute path like this c:\\my uploads\\. That is file will be uploaded to that place.
I am using NetBeans 7 for development. In my Project Tree, I have created another folder called uploads. So when I use relative paths like uploads/ or /uploads/pics/, it is not working. That is, there is no error. But file is not written to that folder. I am using a object of “File” and using its “write()” function.
My project folder structure is like:
-- C:\
-- -- my project\
-- -- -- school\
-- -- -- -- web\
-- -- -- -- -- index.jsp
-- -- -- -- -- fileupload.jsp
-- -- -- -- -- uploads\
-- -- -- -- -- -- pics\
You basically need to convert the relative web path to absolute disk file system path by
ServletContext#getRealPath().However, this approach of writing uploaded files to expanded WAR folder is absolutely not recommended. When you redeploy the WAR, all those uploaded files will get lost (simply because they are not contained in the original WAR!). It will also not work when the server is configured to expand WAR in memory instead of on disk. The
getRealPath()would then returnnull.Write uploaded files to a path outside the expanded WAR folder. You did it right at first place. You can always make the upload folder configureable by specifying it as a VM argument or a properties file setting.
Last but not least, Java code belongs in Java classes, not JSP files.