-
Spring file upload
Hi All!
I used multipart file to upload file in my spring web project.
But anyone out there that knows how to save to a directory inside
your project? I mean for example your web project is named "MyWebProject".
Under that directory is a folder named "Images". I want to upload
in that folder.
Is there a way to do that?
Thanks :D
-
if your project is in a servlet container (webserver like tomcat ) you could do something like this
Code:
// make reference to image directory
String basePath = request.getSession().getServletContext().getRealPath("");
String imageDir = basePath + "/" + "Images";
File dir = new File(imageDir);
// save file
File imageFile = new File(imageDir, imageName);
and the copy multipartfile to newly created file.
Do not forget to set the defaultencoding to UTF-8 of your MultiPartResolver class. Otherwise you can have some nasty bugs :mad: . See http://www.cybersnippet.nl/blog/entr...em_with_spring