Reputation: 11
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.
Upvotes: 0
Views: 2541
Reputation: 349
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:
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" />
Upvotes: 1