title: CORS Error is coming for multipart/form-data in Jersey API
body: <p>I am trying to upload a excel file but CORS error is coming in Jersey API.I am calling the API from angular 4 with multipart/form-data headers and added a filter for each incoming request to JAVA Rest API. Find the code below and help me out:</p>
  
  <p><strong>Angular API CALLING:</strong></p>
  
  <pre><code>export class DboperationService {
  private headers1 = new Headers({'Content-Type' : 'multipart/form-data'});
  sendFile(fileObj: File){
      return this._http.post(this.baseURL+'/submitexcel', fileObj,{headers : this.headers1}).map(res =&gt; res.json().data).subscribe();
    }
  }
  </code></pre>
  
  <p><strong>REST API:</strong></p>
  
  <pre><code>  @POST
             @Path("/submitexcel")
              @Consumes(MediaType.MULTIPART_FORM_DATA)
              public Response uploadFile(
  
                      @FormDataParam("file") InputStream uploadedInputStream,
                      @FormDataParam("file") FormDataContentDisposition fileDetail) {
                  // check if all form parameters are provided
                 System.out.println("CAssked");
                  if (uploadedInputStream == null || fileDetail == null)
                      return Response.status(400).entity("Invalid form data").build();
                  // create our destination folder, if it not exists
                  try {
                      createFolderIfNotExists(UPLOAD_FOLDER);
                  } catch (SecurityException se) {
                      System.out.println("Can not create destination folder on server");
                      return Response.status(500)
                              .entity("Can not create destination folder on server")
                              .header("Access-Control-Allow-Origin", "*")
                              .build();
                  }
                  String uploadedFileLocation = UPLOAD_FOLDER + fileDetail.getFileName();
                  /*try {
                      //saveToFile(uploadedInputStream, uploadedFileLocation);
                  } catch (IOException e) {
                      return Response.status(500).entity("Can not save file").build();
                  }*/
                  System.out.println("File saved to:"+uploadedFileLocation);
                  return Response.status(200)
                          .entity("File saved to " + uploadedFileLocation)
                          .header("Access-Control-Allow-Origin", "*")
                          .build();
  }
  </code></pre>
  
  <p><strong>Cross-Origin Filter:</strong></p>
  
  <pre><code>package com.newgen.aproj2;
  
  
  import com.sun.jersey.spi.container.ContainerRequest;
  import com.sun.jersey.spi.container.ContainerResponse;
  import com.sun.jersey.spi.container.ContainerResponseFilter;
  
  public class CrossOrigin implements ContainerResponseFilter {
  
       @Override
          public ContainerResponse filter(ContainerRequest creq, ContainerResponse cresp) {
  
              cresp.getHttpHeaders().putSingle("Access-Control-Allow-Origin", "*");
              cresp.getHttpHeaders().putSingle("Access-Control-Allow-Credentials", "true");
              cresp.getHttpHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD");
              cresp.getHttpHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
  
              return cresp;
          }
  }
  </code></pre>
  
  <p><strong>Response Headers:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/IBZnp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IBZnp.png" alt="Response Header in Browser"></a></p>