title: How to detect invalid image URL (I mean image is not describe) with JAVA?
body: <p>I have a method to download image from URL. As like below..</p>
  
  <pre><code>public static byte[] downloadImageFromURL(final String strUrl) {
      InputStream in;
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      try {
          URL url = new URL(strUrl);
          in = new BufferedInputStream(url.openStream());
          byte[] buf = new byte[2048];
          int n = 0;
          while (-1 != (n = in.read(buf))) {
              out.write(buf, 0, n);
          }
          out.close();
          in.close();
      }
      catch (IOException e) {
          return null;
      }
      return out.toByteArray();
  }
  </code></pre>
  
  <p>I have an image url and it is valid. for example.</p>
  
  <p><a href="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTxfYM-hnD-Z80tgWdIgQKchKe-MXVUfTpCw1R5KkfJlbRbgr3Zcg" rel="nofollow noreferrer">https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTxfYM-hnD-Z80tgWdIgQKchKe-MXVUfTpCw1R5KkfJlbRbgr3Zcg</a></p>
  
  <p>My problem is I don't want to download if image is really not exists.Like ....</p>
  
  <p><a href="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTxfYM-hnD-Z80tgWdIgQKchKe-MXVUfTpCw1R5KkfJlbRbgr3Zcgaaaaabbbbdddddddddddddddddddddddddddd" rel="nofollow noreferrer">https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTxfYM-hnD-Z80tgWdIgQKchKe-MXVUfTpCw1R5KkfJlbRbgr3Zcgaaaaabbbbdddddddddddddddddddddddddddd</a></p>
  
  <p>This image shouldn't be download by my method. So , how can I know the giving image URL is not really exists. I don't want to validate my URL (I think that may not my solution ). </p>
  
  <p>So, I googled for that. From this article ...
  <a href="https://stackoverflow.com/questions/1378199/how-to-check-if-a-url-exists-or-returns-404-with-java">How to check if a URL exists or returns 404 with Java?</a> and
  <a href="https://stackoverflow.com/questions/4596447/java-check-if-file-exists-on-remote-server-using-its-url">Check if file exists on remote server using its URL</a></p>
  
  <p>But this con.getResponseCode() will always return status code "200". This mean my method will also download invalid image urls. So , I output my bufferStream as like...</p>
  
  <pre><code>System.out.println(in.read(buf));
  </code></pre>
  
  <p>Invalid image URL produces "43". So , I add these lines of codes in my method.</p>
  
  <pre><code>    if (in.read(buf) == 43) {
         return null;
      }
  </code></pre>
  
  <p>It is ok. But I don't think that will always satisfy. Has another way to get it ? am I right? I would really appreciate any suggestions. This problem may struct my head. Thanks for reading my question.</p>
  
  <blockquote>
    <p><strong>*UPDATE</strong></p>
  </blockquote>
  
  <p>I call this download method and save downloaded image in some directory as..</p>
  
  <pre><code>            // call method to save image
              FileSupport.saveFile(filePath+".JPG", data);
  </code></pre>
  
  <p>After that I tried to output as...</p>
  
  <pre><code>            File file = new File(filePath+".JPG);
              System.err.println(file.length());
  </code></pre>
  
  <p>that may also produces <strong>"43</strong>" for invalid image urls. I want to know why that return <strong>"43"</strong> for all of invalid urls. what is <strong>"43"</strong> ?</p>