title: Spring File Upload Fails by 'Access-Control-Allow-Origin' header is present on the requested resource
body: <p>File upload API fails from angularjs post call. Followings is the file upload API code, 
  FileUploadController.java</p>
  
  <pre><code>@RestController
  @RequestMapping(value="/v1")
  public class FileUploadController {
  
      @Autowired
      private ServletContext context;
  
      @RequestMapping(value = "/fileUpload", headers = "content-type=multipart/*", method = RequestMethod.POST)
      public ResponseEntity&lt;FileInfo&gt; upload(@RequestParam("file") MultipartFile inputFile){
          FileInfo fileInfo = new FileInfo();
          HttpHeaders headers = new HttpHeaders();
  
          if(!inputFile.isEmpty()){
              try {
                  String originalFileName = inputFile.getOriginalFilename();
                  File destinationFile = new File(context.getRealPath("/WEB-INF/images") + File.separator + originalFileName);
                  inputFile.transferTo(destinationFile);
                  fileInfo.setFileName(destinationFile.getPath());
                  fileInfo.setFileSize(inputFile.getSize());
                  headers.add("FileUploaded Successfully - ", originalFileName);
  
                  return new ResponseEntity&lt;FileInfo&gt;(fileInfo, headers, HttpStatus.OK);
              } catch (Exception e) {
                  return new ResponseEntity&lt;FileInfo&gt;(HttpStatus.BAD_REQUEST);
              }
          }
          return new ResponseEntity&lt;FileInfo&gt;(HttpStatus.BAD_REQUEST);
      }
  }
  </code></pre>
  
  <p>SimpleCorsFilter.java</p>
  
  <pre><code>    import java.io.IOException;
  import javax.servlet.Filter;
  import javax.servlet.FilterChain;
  import javax.servlet.FilterConfig;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import javax.servlet.http.HttpServletResponse;
  
  public class SimpleCorsFilter implements Filter {
  
      @Override
      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
              throws IOException, ServletException {
          HttpServletResponse response = (HttpServletResponse) res;
          response.setHeader("Access-Control-Allow-Origin", "*");
          response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
          response.setHeader("Access-Control-Max-Age", "3600");
          response.setHeader("Access-Control-Allow-Headers", "x-requested-with, X-Auth-Token, Content-Type");
          response.setHeader("Access-Control-Allow-Credentials","false");
          chain.doFilter(req, res);
      }
  }
  </code></pre>
  
  <p>StackTrace:</p>
  
  <pre><code>SEVERE [http-nio-8080-exec-9] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [appServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found] with root cause
  </code></pre>
  
  <p>Angularjs test code:</p>
  
  <pre><code>myApp.service('fileUpload', ['$http', function ($http) {
      this.uploadFileToUrl = function(file, uploadUrl){
          var fd = new FormData();
          fd.append('file', file);
          $http.post(uploadUrl, fd, {
              transformRequest: angular.identity,
              headers: {'Content-Type': undefined}
          })
          .success(function(){
          })
          .error(function(){
          });
      }
  }]);
  
  myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
  
      $scope.uploadFile = function(){
          var file = $scope.myFile;
          console.log('file is ' );
          console.dir(file);
          var uploadUrl = "http://xxx/v1/fileUpload";
          //var uploadUrl = "http://localhost:8080/xxx/v1/fileUpload";
          fileUpload.uploadFileToUrl(file, uploadUrl);
      };
  
  }]);
  </code></pre>
  
  <p>Issue:
   No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<a href="http://localhost" rel="nofollow noreferrer">http://localhost</a>' is therefore not allowed access. The response had HTTP status code 500.</p>
  
  <p>Please help me to rectify this issue</p>