I want to upload an image file in jsp. My IDE is eclipse. Unfortunately the file is uploading to the path where Eclipse.exe resides. I want to upload it in my project folder of Workspace. How to change the fileUpload By default path. Help please
-
1Please think about what your application will have to do when deployed on a production server. There won't be any Eclipse then. – JB Nizet Mar 11 '12 at 08:28
1 Answers
You should not use relative paths in File
:
File file = new File("filename.ext");
You never know for sure where it will end up. Even more, wherever it will end up may not be writable at all, or it may be completely erased whenever the webapp redeploys or the server restarts. You have no control over this from inside the webapp.
You should use an absolute path which resides outside the project folder and deploy folder which the serveradmin has to create:
File file = new File("/path/to/all/uploads", "filename.ext");
On Windows with the server running on C:\
disk, it will then end up in C:\path\to\all\uploads
. You only need to make sure that this folder is already been prepared.
You should also design your webapp as such that this absolute path is externally configureable so that you don't need to hardcode the path and recompile the code everytime whenever the path changes. For example, as a VM argument or an environment variable.
-Dupload.location=/path/to/all/uploads
You can then retrieve this folder as follows:
String uploadLocation = System.getProperty("upload.location");
// ...
File file = new File(uploadLocation, "filename.ext");