I’m really new to Spring and Spring MVC and I’m working on a kind of test program that uploads a file, writes it to the servlet’s temporary working directory, and then passes that file to the rest of the program (which is a Spring Batch Job and works fine). My problem currently is figuring out how to get the location of the temporary file within my Controller used to configure Spring’s DispatcherServlet.
The code I’ve seen online to get the temporary file location is this:
File directory = (File)getServletContext().getAttribute("javax.servlet.context.tmpdir");
File file = File.createTempFile("prefix", ".tmp", directory);
FileWriter out = new FileWriter(file);
But from what I’ve seen so far it’s not possible to use getServletContext in the Controller class I’m using to configure the servlet.
Any advice on how to get what I want or on other ways to handle this are greatly appreciated. If you need any more details just ask.
Did you try injecting
ServletContextinto your controller with@Autowiredannotation and access it in your method.