0

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.

1
  • 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. Commented Aug 29, 2015 at 7:42

1 Answer 1

1

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" /> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.