Results 1 to 2 of 2

Thread: Spring file upload

  1. #1
    Join Date
    Sep 2008
    Posts
    13

    Default 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

  2. #2

    Default

    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 . See http://www.cybersnippet.nl/blog/entr...em_with_spring

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •