Announcement Announcement Module
No announcement yet.
Spring file upload Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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?


  • #2
    if your project is in a servlet container (webserver like tomcat ) you could do something like this

    // 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 . See