Problem Context:
I am trying to persist a file, uploaded by the user, to file system.
Description:
Actually, I am using Spring MVC (3.0) and this is how I am trying to persist the file,
String orgName = file.getOriginalFilename();
String filePath = "/Uploads/MyUploads/"+ new Date().getTime()+orgName;// Unique Image Name
File dest = new File(filePath);
file.transferTo(dest);
I have a folder Uploads\MarketplaceUploads, at the root of my application. But still I am getting FileNotFoundException "\Uploads\MyUploads\loading.gif (The system cannot find the path specified)".
Below is the directory structure:

You should not store uploaded files in public web content.
Store them outside the server's webapps folder. Provide the root of this location as an absolute disk file system path to your webapp, e.g. by a properties file or a VM argument. An alternative is to store them in a database, this is often the only solution if it's a 3rd party host which doesn't offer access to a fixed location outside the web content.
Ensure you add the web-app context path before using the file
String orgName = file.getOriginalFilename();
String filePath = getServletContext().getRealPath("/") + "/Uploads/MyUploads/"+ new Date().getTime()+orgName;// Unique Image Name
File dest = new File(filePath);
file.transferTo(dest);
From the javadoc of getRealPath()
Gets the real path corresponding to the given virtual path. For example, if path is equal to /index.html, this method will return the absolute file path on the server's filesystem to which a request of the form http://://index.html would be mapped, where corresponds to the context path of this ServletContext. The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. Resources inside the /META-INF/resources directories of JAR files bundled in the application's /WEB-INF/lib directory must be considered only if the container has unpacked them from their containing JAR file, in which case the path to the unpacked location must be returned. This method returns null if the servlet container is unable to translate the given virtual path to a real path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With