Announcement Announcement Module
Collapse
No announcement yet.
How to get file name from CosMultipartResolver Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get file name from CosMultipartResolver

    I have been using the org.springframework.web.multipart.cos.CosMultipart Resolver and binding the file bytes to a command class with initBinder e.g. binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());

    This gets me the file bytes, but I also need the file name. Is there are way to do this? I tried subclassing ByteArrayMultipartFileEditor and can reference the file name, but not sure how to make it accessible in onSubmit(Object command) method. Thanks

  • #2
    I use the commons multipart file resolver (make sure you have commons-fileupload.jar in your classpath):-

    Code:
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    MyCommand object:-

    Code:
    import org.springframework.web.multipart.MultipartFile;
    
    ...snip...
    
    /**
     * Sets the image
     *
     * @param image The image
     */
    public void setImage(MultipartFile image)
    {
        this.image = image;
    }
        
    /**
     * Retrieves the image
     *
     * @return The image
     */
    public MultipartFile getImage()
    {
        return (image);
    }
    MultipartFilePropertyEditor:-

    Code:
    /*
     * MPSC-Spring - A library of common code to use with the Spring framework
     *
     * Copyright 2005 Matt Parker
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    package uk.co.mpcontracting.modules.spring.support;
    
    import java.beans.PropertyEditorSupport;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    import org.springframework.web.multipart.MultipartFile;
    
    /**
     * An implementation of <code>java.beans.PropertyEditor</code> that supports a Spring
     * multipart file
     *
     * @author Matt Parker ([email protected])
     */
    public class MultipartFilePropertyEditor extends PropertyEditorSupport
    {
        private static Log log = LogFactory.getLog(MultipartFilePropertyEditor.class);
        
        /**
         * Sets the multipart file into the property editor
         *
         * @param value The multipart file
         */
        public void setValue(Object value)
        {
            if (log.isDebugEnabled())
            {
                if (value != null)
                {
                    log.debug("Object = " + value.getClass().getName());
                }
                else
                {
                    log.debug("Object = null");
                }
            }
            
            if (value instanceof MultipartFile)
            {
                super.setValue(value);
            }
            else if (value != null)
            {
                log.error("Object is not a multipart file - " + value.getClass().getName());
            }
        }
    
        /**
         * Overrides the set as text method so no exceptions are thrown
         *
         * @param text The text
         * @throws IllegalArgumentException If either the String is
         * badly formatted or if this kind of property can't be expressed
         * as text
         */
        public void setAsText(String text) throws IllegalArgumentException 
        {
    	return;
        }
        
        /**
         * Retrieves a textual representation of the multipart file from the property editor
         *
         * @return A textual representation of the multipart file
         */
        public String getAsText()
        {
            Object value = getValue();
            
            if (value != null)
            {
                return (((MultipartFile)value).getOriginalFilename());
            }
            
            return ("");
        }
    }
    In initBinder:-

    Code:
    binder.registerCustomEditor(MultipartFile.class, new MultipartFilePropertyEditor());
    In my form controller:-

    Code:
    MyCommand myCommand = (MyCommand)command;
    
    MultipartFile image = myCommand.getImage();
    String fileName = image.getOriginalFilename()
    HTH,

    Bob

    Comment


    • #3
      Hi Bob,

      when is the function setImage() of your command class called ???

      4had

      Comment


      • #4
        Originally posted by 4had
        Hi Bob,

        when is the function setImage() of your command class called ???

        4had
        Spring will call it when the file is uploaded. The command class is registered as the command of the form controller.

        So in my JSP I'd have an <input type="file" name="image"> and the file will end up in the command object - it's translated from the raw bytes by Spring because I've registered the MultipartFilePropertyEditor in initBinder.

        Then in your validate method you'd have:-

        Code:
        public void validate(Object obj, Errors errors)
        {
            MyCommand myCommand = (MyCommand)obj;
        
            MultipartFile image = myCommand.getImage();
            String fileName = image.getOriginalFilename();
        
            ...validate the filename here
        }
        Bob

        Comment


        • #5
          Ok, now I understand.

          Thank you.

          Comment

          Working...
          X