title: Why isn't the image displaying in the client browser when sent via a socket write?
body: <p>I am creating a simple HTTP server (with maven).  When the user enters <code>http://127.0.0.1:8181/pic</code>, an image should be displayed.</p>
  
  <p>When I run the class and enter <code>http://127.0.0.1:8181/pic</code>, the image doesn't display; I just get lines of garbage characters instead!</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      int port = 8181;
  
      ServerSocket server = new ServerSocket(port);
      System.out.println("Listening for a connection on port " + port + " ...");
  
      while (true) {
          Socket client = server.accept();
          System.out.println("A client connected: " + client.getInetAddress());
  
          BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
          BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
  
          String line = in.readLine();
          while (!line.isEmpty()) {
              System.out.println(line);
  
              String[] firstLine = line.split("\\s+"); // line is like: GET /pic HTTP/1.1
              String method = firstLine[0];
  
              if (method.equalsIgnoreCase("GET")) {
  
                  String resourceNme = firstLine[1];
  
                  if (resourceNme.equals("/pic")) {
  
                      out.write("Server: Apache/0.8.4\r\n");
                      out.write("Content-Type: text/html\r\n");
                      out.write("Content-type: image/gif");
  
                      out.write("\r\n");
                      out.write("&lt;TITLE&gt;Example Image&lt;/TITLE&gt;");
  
                      //find file in system and send as response
                      URL url = SimpleHTTPServer.class.getResource("/office.jpg");
                      File file = new File(url.toURI());
  
                      if (file.exists()) {
                          System.out.println("file exists");
                          ImageInputStream imageInputStream = ImageIO.createImageInputStream(file);
                          long size = imageInputStream.length();
  
                          BufferedImage bufferedImage = ImageIO.read(file);
                          boolean success = ImageIO.write(bufferedImage, "jpeg", client.getOutputStream());
                          System.out.println("Success: " + success); // is true
                      } else {
                          System.out.println("don't exists");
                      }
  
                      out.close();
                      in.close();
                      client.close();
                      break;
  
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Sample output received:</strong></p>
  
  <pre><code>SHùiÆƒ@8¤ÇîÔéHŽ”c­;… äúÑ“ëIŠ1@ËQ“ï@¥Å��œ÷¥Éõ¢Š�9õ¥Éõ¤¥ 'Ö€ÄzÑF(wzFb;ÑPÈø¤d”Œòj³HÄõ4®ù¨è»›ûÆÍýãIE�.æþñ£sxÒb—�noï7·÷£�ííêi|Æþñ¦âœ�Û?xÒïoïLRâ€
  íýãNÞßÞ4ÜRâ€{xÑ½¿¼i1F)�»›ûÆÍýãE�noSFæõ4bŒPîoïPíýãH ooï77÷ ¸ ÙûÆ—sxÓ@æ—�»›ûÆ—-ëMÅ--ýãFæõ4QŠ�]ÍêiK6:ÒRã4Ä"ï,�'šé4]5‹od•Ÿ§é­1G*H&lt;×saf©œ`ŽÕ¤DÉ µÅXÆÓš~=iÈÅjf0¯9 ¨àŠv21@ 2äç¸ÈÎ)øãÆ)€ÜdRàÆ)zR‚E �7-�”v P�(�r)hÆ
  0œbŒu¥ÆiHç4�˜”`n¥Åw Ú3JGJZ"€G|R°Î)våhÆåÅ�&gt;ZR2™ |Ê&gt;”ª3=)0
  £f=©/ð§'+BŒ(c�¸¸Åâ€Œ
  </code></pre>
  
  <p><strong>Updated Code with HTML-related response codes removed</strong></p>
  
  <pre><code>                if (resourceNme.equals("/pic")) {
                      out.write("Server: Apache/0.8.4\r\n");
                      out.write("Content-type: image/jpeg\r\n");
  
                      URL url = SimpleHTTPServer.class.getResource("/office.jpg");
                      File file = new File(url.toURI());
  
                      if (file.exists()) {
                          System.out.println("file exists");
                          ImageInputStream imageInputStream = ImageIO.createImageInputStream(file);
                          long size = imageInputStream.length();
                          out.write("Content-Length: " + size + "\r\n");
                          out.write("\r\n");
  
                          BufferedImage bufferedImage = ImageIO.read(file);
                          boolean success = ImageIO.write(bufferedImage, "jpeg", client.getOutputStream());
                          System.out.println("Success: " + success); //true
                          out.close();
                          in.close();
                          client.close();
                          break;
                      } else {
                          System.out.println("don't exists");
                          out.close();
                          in.close();
                          client.close();
                          break;
                      }
                  }
  </code></pre>