Pregunta

I work with Spring.

I have this code on my controller

@Controller
public class FileUploadController {

    @RequestMapping(value = "/file-upload", method = RequestMethod.POST)
    @ResponseBody
    public String uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request,ServletContext context) throws IOException {

        if(!file.isEmpty()) {

            file.transferTo(new File("/data.xml"));     
            return "ok";
        } 
        else {
            return "Empty";
        }
    }
}

i just want to save the uploaded file to the root directory.

It seem to me that i use a absolute Path to the Root, but i don't now how i can find that out.

Can anybody help me ?

No hay solución correcta

Otros consejos

You can get real path with HttpServletRequest.getSession().getServletContext() but this will be helpful If your application has session.If your application is sessionless than you can do by implementing ServletContextAware.

If application has session then

request.getSession().getServletContext().getRealPath("WEB-INF");

also one another way to get real path.

Get the class loader from one of your objects and use that to get its local path. That will give you a path from your execution point in Tomcat. From there you can find the location you want.

obj.getClass().getResource("/");

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top