MDP
MDP

Reputation: 4267

Where saving a file after being uploaded in Spring MVC

I have a form the allow user to upload file (jpg) with <form:input type="file"/> in SPring MVC

When I save the file I don't get any error from this code, but the file is not saved in C:\testTile.jpg:

   public void salvaFoto(CommonsMultipartFile foto){
        ...
        foto.transferTo(new File("C:\\testTile.jpg")); //This path is
         //just a try, of course I should save the file in my Application url
        ...
    }

My question is: how should I retrieve the application path in order to have the URI for my File object? I tried with ServletContext.getContextPath(), but the file wasn't saved as well.

Thank you

Upvotes: 0

Views: 229

Answers (1)

Naruto
Naruto

Reputation: 4329

When I save the file I don't get any error from this code, but the file is not saved in C:\testTile.jpg:

The problem is as you don't have enough permission to write to that path.

Run your application in debug mode and mark breakpoint before this call and check the path that it is showing. You will be able to debug the solution by your own using debug mode.

This might help if you want to store inside WEB-INF/../ folder of your application

String path = getServletContext().getRealPath("WEB-INF/../");
File file = new File(path);
String fullPathToYourWebappRoot = file.getCanonicalPath();

Upvotes: 1

Related Questions