title: Base64 image encoded in Java not displaying in HTML
body: <p>I've encoded a image into base64 using the <a href="https://axis.apache.org/axis/java/apiDocs/org/apache/axis/encoding/Base64.html" rel="nofollow">org.apache.axis.encoding.Base64</a> class. My code:</p>
  
  <pre><code>public String get64EncodedPhotoForUser(User user) {
  
          // Path to image
          String path = "some path to the jpg file on the server";
  
          // Read image from server
          String base64Image = null;
  
          try {
  
              BufferedImage bufferedImage = ImageIO.read(new File(path));
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              ImageIO.write(bufferedImage, "jpg", baos);
              baos.flush();
              byte[] imageInByte = baos.toByteArray();
              baos.close();
              base64Image = Base64.encode(imageInByte);
  
          } catch (Exception e) {
              System.out.println(e.getMessage());
          }
  
          return base64Image;
  
      }
  </code></pre>
  
  <p>As you can see I first convert the image into a byte array as that is what the encode method expects.. Everything seems to work fine, I get my encoded string back and it seems to be a correctly encoded string. Worth mentioning here that some online decoders can correctly decode the image while others again can't, which I thought is odd..</p>
  
  <p>When I try and display the image in my HTML:</p>
  
  <pre><code>&lt;img ng-src="data:image/png;base64,/9j/4AAQSkZJRgABAgAAA..." id="avatar"&gt;
  </code></pre>
  
  <p>It doesn't work. I just get that 'missing image' icon you get when you're pointing your html to an image that doesn't exist.</p>
  
  <p>I've copied and pasted the encoded string into a jsfiddle you can check out <a href="https://jsfiddle.net/h9d96oyk/" rel="nofollow">here</a></p>
  
  <p>PS - I also take the base64 string and I pass it from the server to the client in a JSON object along with some other things. The Base64 string you're seeing in the jsfiddle is what I get back on the client-side. Could it be that the encoded string could perhaps have been corrupted or modified, when it was passed over the network?</p>