title: File upload in struts2 using s:file
body: <p>I am trying to upload the file using S:file tag but it is giving me http 500 internal error. But When I directly give filename and path as constant in same code it works. So I am not sure where I am missing the loop hole.</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%&gt;
  &lt;%@ taglib prefix="s" uri="/struts-tags"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Struts 2 - Login Application | ViralPatel.net&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;h2&gt;Struts 2 - Login Application&lt;/h2&gt;
  &lt;s:actionerror  /&gt;
  &lt;s:form action="uploadFile.action"  method="post" enctype="multipart/form-data" &gt;
  &lt;s:textfield name="myName"&gt;&lt;/s:textfield&gt;
    &lt;s:file name="myFile"&gt;&lt;/s:file&gt;
      &lt;s:submit  value="Submit" align="center" /&gt;
  &lt;/s:form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Action</p>
  
  <pre><code>class FileUploadAction extends ActionSupport implements ServletRequestAware {
      File userImage;
      String userImageContentType;
      String userImageFileName;
      HttpServletRequest servletRequest;
  
      public String execute() {
          try {
              String filePath = servletRequest.getSession()
                      .getServletContext().getRealPath("/");
              File fileToCreate = new File(filePath, this.userImageFileName);
              FileUtils.copyFile(this.userImage, fileToCreate);
          } catch (Exception e) {
              addActionError(e.getMessage());
              return INPUT;
          }
          return SUCCESS;
      }
  
      public void setServletRequest(HttpServletRequest servletRequest) {
          servletRequest = servletRequest;
      }
  }
  </code></pre>
  
  <p>Config</p>
  
  <pre><code>&lt;action name="userImage" class="net.viralpatel.struts2.FileUploadAction"&gt; 
      &lt;interceptor-ref name="fileUpload"&gt; 
          &lt;param name="maximumSize"&gt;2097152&lt;/param&gt; 
          &lt;param name="allowedTypes"&gt;
                    image/png,image/gif,image/jpeg,image/pjpeg
              &lt;/param&gt; 
      &lt;/interceptor-ref&gt; 
      &lt;interceptor-ref name="defaultStack"&gt;
      &lt;/interceptor-ref&gt; 
      &lt;result name="success"&gt;SuccessUserImage.jsp&lt;/result&gt; 
      &lt;result name="input"&gt;UserImage.jsp&lt;/result&gt; 
  &lt;/action&gt;
  </code></pre>
  
  <hr>
  
  <p>EDIT: next time edit your post instead of posting code in comments...</p>