You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Yann Verlynde <yv...@oxade.com> on 2002/11/04 16:41:15 UTC

Problem with FileUpload

Hello,

This is my code in an Action Class, I don't receive the upload file. The fileItems element is null.
How can I do to upload a file?

Thanks in advance

public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        throws Exception {

   try {
      FileUpload fu = new FileUpload();
      // maximum size before a FileUploadException will be thrown
      fu.setSizeMax(1000000);
      // maximum size that will be stored in memory
      fu.setSizeThreshold(4096);
      // the location for saving data that is larger than getSizeThreshold()
      fu.setRepositoryPath("/tmp");

      List fileItems = fu.parseRequest(request);
      // assume we know there are two files. The first file is a small
      // text file, the second is unknown and is written to a file on
      // the server
      Iterator i = fileItems.iterator();
      String comment = ((FileItem)i.next()).getString();
      FileItem fi = (FileItem)i.next();

      // filename on the client
      String fileName = fi.getFieldName();
      // save comment and filename to database

      MessageResources messages = (MessageResources)getServlet().getServletContext().getAttribute("APPLICATION_CONFIGURATION");

      // write the file
      fi.write(messages.getMessage("upload.destination") + fileName);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    //return a forward to display.jsp
    return mapping.findForward("display");
}