title: How can I display images from a HTTP Web Server?
body: <p>I am trying to display images inside my HTTP Web Server but I'm unable to. I can display the HTML. I think it is something to do with the way I handle my IO (input and output streams). There's probably a lot of mistakes in there that I haven't noticed.</p>
  
  <pre><code>import java.io.* ;
  import java.net.* ;
  import java.util.Properties;
  
  
  public class HTTPThread extends Thread
  {
      private Socket socket = null;
      private Properties config = null;
      private String root = "";
  
      public HTTPThread(Socket s, Properties config)
      {
          this.socket = s;
          this.config = config;
          this.root = this.config.getProperty("root");        
      }
  
      public void run()
      {   
  
  //      InputStream in = null;
          OutputStream out = null;
  
          try
          {
              out = socket.getOutputStream();
              PrintWriter writer = new PrintWriter(out, true);
  
              BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              String request = reader.readLine();
              writer.println("HTTP/1.1 200 OK");
              writer.println("Content-Type: text/html");          
              writer.println();
  
              // Search for filename 
              int slash = request.indexOf("/"); //find first occurrence of slash
              int emptySpace = request.indexOf(" ", slash); //find first space after slash
              String filename = request.substring(slash, emptySpace); //return filename 
  //          writer.println("&lt;b&gt;" + filename + "&lt;/b&gt;");
  
              String pathname = "";
              try
              {
                  pathname = (filename == "/") ? root + filename : root;
  //              System.out.println(filename);           
                  URL url = new URL(pathname);
                  URLConnection urlc = url.openConnection();
                  BufferedReader in = new BufferedReader(
                          new InputStreamReader(
                          urlc.getInputStream()));
  
                  String line;                
                  while ((line = in.readLine()) != null)
                  {
                      writer.println(line);
                  }
                  in.close();
              }
              catch (MalformedURLException e)
              {
                  System.err.println("Don't know about host: " + pathname);
                  System.exit(1);
              }
              catch (IOException e)
              {
                    System.err.println("Couldn't get I/O for "
                                       + "the connection to: " + pathname);
                    System.exit(1);
              }
  
  
  
  //          reader.close();
              writer.close();
              socket.close();
          }
          catch(IOException e)
          {
              System.out.println("Error: " + e);          
          }
  
          finally
          {
              try
              {
  //              in.close() ;
                  out.close() ;
                  socket.close();
              }
              catch(IOException e)
              {
                  System.out.println("Error: " + e);          
              }
          }
      }
  }
  </code></pre>