title: CORS issue while downloading Image
body: <p>I have an API which converts base64 string to image and write the image in Tomcat Server.The image writes successfully after calling API but gives the error while retrieving the same image follows:</p>
  
  <blockquote>
    <p>"No 'Access-Control-Allow-Origin' header is present on the requested
    resource. Origin is therefore not allowed access. XMLHttpRequest
    cannot load <a href="http://hostname:4444//tmpFiles/31487660124865.jpg" rel="nofollow noreferrer">http://hostname:4444//tmpFiles/31487660124865.jpg</a>. No
    'Access-Control-Allow-Origin' header is present on the requested
    resource.</p>
  </blockquote>
  
  <p>My code is as follows:</p>
  
  <pre><code>public Message uploadImage(Map&lt;String, String&gt; map) {
  
      // Initializing the message
      Message message = new Message();
  
      try {
  
          // Get the file data
          String fileData = map.get("file_data");
  
          // Split the data with the comma
          String base64Image = fileData.split(",")[1];
  
          // Convert the base64 input to binary
          byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image);
  
          BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
  
          // Manipulations in File Name
          String fileName = map.get("file_name");
          String file = fileName.substring(0, fileName.indexOf("."));
          String fileExtension = fileName.substring(fileName.indexOf("."));
  
          // Get the current time
          Long time = new Date().getTime();
  
          // Write the file name with the current time to avoid redundancy
          String maniputedFileName = file + "" + time + fileExtension;
          System.out.println("manipulated file name is " + maniputedFileName);
  
          // Check if file name is not empty
          if (!maniputedFileName.isEmpty()) {
  
              // Get the root path of tomcat server
              String rootPath = System.getProperty("catalina.home");
  
              System.out.println("root Path:- " + rootPath);
  
              // File Directory/Path on tomcat server
              File fileDirectory = new File(rootPath + File.separator + "webapps/tmpFiles");
  
              // If file direcory does not exist
              if (!fileDirectory.exists())
                  fileDirectory.mkdirs();
  
              File outputfile = new File(fileDirectory.getAbsolutePath() + File.separator + maniputedFileName);
  
              // Write created image on server
              ImageIO.write(image, "png", outputfile);
  
              // Set the success message
              message.setDescription("You successfully uploaded file=" + maniputedFileName);
              message.setObject(outputfile.getAbsolutePath());
              message.setValid(true);
              return message;
  
          }
          // Set the failure message
          else {
              message.setDescription("You failed to upload " + maniputedFileName + " because the file was empty.");
              message.setValid(false);
              return message;
          }
      }
      // Handling all exceptions
      catch (IOException e) {
          message.setDescription(e.getMessage());
          message.setValid(false);
          return message;
      }
  }
  </code></pre>
  
  <p>And web.xml is:</p>
  
  <pre><code>     &lt;filter&gt;
          &lt;filter-name&gt;tokenfilter&lt;/filter-name&gt;
          &lt;filter-class&gt;com.springiot.filter.TokenFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
      &lt;filter-name&gt;tokenfilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;SimpleCORSFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;com.springiot.filter.SimpleCORSFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;SimpleCORSFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>And my tokenFilter Class is :</p>
  
  <pre><code> HttpServletResponse response = (HttpServletResponse) res;
  
      response.addHeader("Access-Control-Allow-Origin", "*");
      response.addHeader("Access-Control-Allow-Methods", "POST, GET,OPTIONS, DELETE");
      response.addHeader("Access-Control-Max-Age", "3600");
      response.addHeader("Access-Control-Allow-Headers",
              "Content-Type, Access-Control-Allow-Headers, Authorization,X-Requested-With,token,userKey,user_id");
  </code></pre>