title: Java get image extension from byte array
body: <p>I have the below code for saving an image from a byte array.</p>
  
  <p>I am able to save the image successfully using the below code.</p>
  
  <p>Currently I am saving image with ".png" format but I want to get the image extension from byte array and save image with this extension.</p>
  
  <p>Here is my code</p>
  
  <pre><code>public boolean SaveImage(String imageCode) throws Exception {
      boolean status = false;
      Connection dbConn = null;
      CallableStatement callableStatement = null;
      try {
          String base64Image = imageCode.split(",")[1];
          byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image);
  
          Properties propFile = LoadProp.getProperties();
          String filepath = propFile.getProperty(Constants.filepath);
          File file = new File(filepath + "xyz.png");
          FileOutputStream fos = new FileOutputStream(file);
          try {
              fos.write(imageBytes);
          } finally {
              fos.close();
          }
      } catch (Exception e) {
          throw e;
      } finally {
          if (callableStatement != null) {
              callableStatement.close();
          }
          if (dbConn != null) {
              dbConn.close();
          }
      }
      return status;
  }
  </code></pre>
  
  <p>I am using Java and Tomcat 8.</p>