I am creating a spring web project where i am uploading a csv file and saving it to database. I need to keep the file in the relative path of the project so that it can be accessed through the url.for example: localhost:port/project_name/file_name But I am getting the absolute path everytime using servlet context or URL. Please help me out to get the relative path in spring controller.
- The CSV data is in the database. So there is no file at all. All you need is a controller action, mapped to /{csvFileId}.csv for example, that will get the ID of the csv file from the path variable, get the data from the database and send them to the browser.JB Nizet– JB Nizet2015-08-29 07:42:39 +00:00Commented Aug 29, 2015 at 7:42
Add a comment |
1 Answer
You can save the file wherever you want. I particularly create a folder in the tomcat's directory and access it through the Java System Property System.getProperty("catalina.base");
Then to the url you can choose one of these possibilities:
- Create a controller that serves the file.
- Declare an Context in tomcat: option1 or option2
For example, I saved the file in:
System.getProperty("catalina.base")+File.separator+"mydata"+File.separator+filename; I can create the controller:
@Controller public class MyDataController { @RequestMapping("/mydata/{filename}") public String helloWorld(@PathVariable("filename") String filename) { String path = System.getProperty("catalina.base")+File.separator+"mydata"+File.separator+filename; return new FileSystemResource(new File(path)); } } or declare a context in tomcat create the file: [tomcat6directory]/conf/Catalina/localhost/appcontext#mydata.xml containing
<Context antiResourceLocking="false" privileged="true" path="/mydata" docBase="${catalina.base}/mydata" />