martinc     2002/07/30 23:43:18

  Modified:    src/share/org/apache/struts/upload
                        CommonsMultipartRequestHandler.java
  Log:
  Fix a compatibility issue with DiskMultipartRequestHandler, so that
  FormFile.getFileName() returns the base file name, instead of the full
  (client-relative, and therefore not useful) path to the uploaded file.
  
  Submitted by: Erich Meier
  
  Revision  Changes    Path
  1.2       +38 -5     
jakarta-struts/src/share/org/apache/struts/upload/CommonsMultipartRequestHandler.java
  
  Index: CommonsMultipartRequestHandler.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/upload/CommonsMultipartRequestHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CommonsMultipartRequestHandler.java       27 Jul 2002 21:53:13 -0000      1.1
  +++ CommonsMultipartRequestHandler.java       31 Jul 2002 06:43:18 -0000      1.2
  @@ -545,7 +545,7 @@
            * @return The client-size file name.
            */
           public String getFileName() {
  -            return fileItem.getName();
  +            return getBaseFileName(fileItem.getName());
           }
   
   
  @@ -599,6 +599,39 @@
            */
           public void destroy() {
               fileItem.delete();
  +        }
  +
  +
  +        /**
  +         * Returns the base file name from the supplied file path. On the surface,
  +         * this would appear to be a trivial task. Apparently, however, some Linux
  +         * JDKs do not implement <code>File.getName()</code> correctly for Windows
  +         * paths, so we attempt to take care of that here.
  +         *
  +         * @param filePath The full path to the file.
  +         *
  +         * @return The base file name, from the end of the path.
  +         */
  +        protected String getBaseFileName(String filePath) {
  +
  +            // First, ask the JDK for the base file name.
  +            String fileName = new File(filePath).getName();
  +
  +            // Now check for a Windows file name parsed incorrectly.
  +            int colonIndex = fileName.indexOf(":");
  +            if (colonIndex == -1) {
  +                // Check for a Windows SMB file path.
  +                colonIndex = fileName.indexOf("\\\\");
  +            }
  +            int backslashIndex = fileName.lastIndexOf("\\");
  +
  +            if (colonIndex > -1 && backslashIndex > -1) {
  +                // Consider this filename to be a full Windows path, and parse it
  +                // accordingly to retrieve just the base file name.
  +                fileName = fileName.substring(backslashIndex + 1);
  +            }
  +
  +            return fileName;
           }
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to