Results 1 to 7 of 7

Thread: MutliPartFile.getOriginalFilename() full windows path IE

  1. #1
    Join Date
    Aug 2004
    Posts
    3

    Default MutliPartFile.getOriginalFilename() full windows path IE

    I'm using this code to save a MultiPartFile (file) to the filesystem.

    Code:
    file.transferTo(new File("/path/to/dir/" + file.getOriginalFilename()));
    It works fine and saves the file BUT when I use IE to upload the file the filename includes the full local windows path so files are being saved with names like D:\my\files\thefile.gif instead of thefile.gif

    Uploading with Firefox does not have this problem. I know the Javadoc mentions that this may happen in Opera. Has anyone had this problem (and hopefully solved it) before?

    Thanks
    Steve

  2. #2
    Join Date
    Aug 2004
    Location
    Madrid, Spain
    Posts
    25

    Default

    I usually do something like the following:
    Code:
    File f = new File(file.getOriginalFilename());
    String fname = f.getName();
    file.transferTo(new File("/path/to/dir/" + fname));
    Hope this helps,
    F.

  3. #3
    Join Date
    Aug 2004
    Posts
    3

    Default

    Thanks Fernando, tried it but I'm afraid it still seems to do the same thing. I guess I'm just going to have to check the getOriginalFilename() string for \s before I use it. Something like...

    Code:
    String filename = file.getOriginalFilename();
    
    if (filename.indexOf("\")!=-1)
        // trim out the windows file path...
    }

  4. #4
    Join Date
    Aug 2004
    Location
    Madrid, Spain
    Posts
    25

    Default

    Oh, I think I see where my error is.

    Most likely you are using some non-Windows platform at the server side. Since File specifications are not portable across platforms, the server-side created java.io.File is not being able to extract the name from the supplied client-side Windows path.

    Yes, I'm afraid you'll have to write the code to extract the name... now I'll go back to correct my own code :wink: !

    Rgds,
    F.

  5. #5
    Join Date
    Aug 2004
    Posts
    3

    Default

    OK! Thanks for your help

  6. #6
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    I think this is due to a pretty well known bug in commons-fileupload, which can't handle the situation where the client is a different OS than the server.

    This is described to some extent in this Tapestry bug database entry:

    http://issues.apache.org/bugzilla/show_bug.cgi?id=27544

    Regards,
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  7. #7
    Join Date
    Aug 2004
    Location
    Linz, Austria
    Posts
    391

    Default

    I've just added added special parsing of original filenames to Spring's CommonsMultipartFile, detecting both Unix- and Windows-style paths. It's trivial to do, and Commons FileUpload still doesn't seem to fix this itself.

    Juergen

Similar Threads

  1. Replies: 4
    Last Post: May 2nd, 2007, 08:55 PM
  2. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  3. Replies: 0
    Last Post: Sep 5th, 2005, 08:49 AM
  4. Replies: 2
    Last Post: Jul 21st, 2005, 04:07 AM
  5. Resource: Add valid path not found
    By moacsjr in forum Data
    Replies: 3
    Last Post: May 24th, 2005, 05:53 PM

Posting Permissions

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