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

  • problem with file upload

    I am getting ClassCastException while trying to cast HttpServletRequest object into DefaultMultipartHttpServletRequest in my controller that extends SimpleFormController.

    I have debugged and determined that the request object is type org.apache.catalina.connector.RequestFacade class.

    The following is my "resolver" my config xml
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.C ommonsMultipartResolver"/>

    This is the part from jsp :
    <td><input type="file" size="100" name="fileData" id="fileData"/></td>
    <td align=center><input class="exec-button" type=button value="Upload" onclick="fnUploadFile();"></td>

    function fnUploadFile(){
    document.uploadForm.enctype = "multipart/form-data";


    This is my "command" bean:
    public class FileData implements Serializable {
    private byte[] fileData;
    I also set custom binder to bind array class.

    This is from my controller :
    protected ModelAndView onSubmit( HttpServletRequest req,
    HttpServletResponse res,
    Object o,
    BindException errors) throws Exception {
    UploadData uploadData = (UploadData )o;
    byte[] fileByteArray = uploadData.getFileData();

    Class reqClass = req.getClass();

    DefaultMultipartHttpServletRequest multipartRequest = (DefaultMultipartHttpServletRequest) req;
    CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("fileData");

    I am getting byte array with content of uploaded file,but then the "cast" exception is thrown.

    I need that cast because I need to get the name of uploaded file.

    Please help to find out what I did incorrect.


  • #2
    I just determined that byte array i receive contain actually the file path name displayed in the "text" box located next to "browse" button on jsp, and not the content of the uploaded file.

    It means that I definitely do something wrong.

    Please help.

    Thank you.


    • #3
      I bet you forgot to add


      to the <form> tag in your JSP.


      • #4
        No, i did not. The problem was that "enctype" attribute of form for IE browser should be set via "encoding" and not "enctype".

        Anyway thanks for response.