title: How to download an image using Java Socket from a remote Http server?
body: <p>I have been trying to implement a simple http client using Java Socket. In my program I am requesting an Image from a server and trying to copy the requested JPEG image on local machine. I have managed to construct the request and received the desired content. I have also separated the response header and the content. But the problem is when I write  the bytes using <code>FileOutputStream</code> into a .jpeg file and after writing when open the file in a image viewer (like picasa) the image seems to be invalid.
  Here is my entire code.
  Can anyone plz tell me what's wrong with code? Why the image is invalid? </p>
  
  <pre><code>import java.io.BufferedInputStream;
  import java.io.BufferedReader;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.Socket;
  
  import sun.misc.IOUtils;
  
  
  public class ImageCopy {
     public static void main(String args[]) throws IOException{
                 String host = "www.uni-koblenz-landau.de";   //declare the host name
                 String resourceLoc = "/images/starts-c-ko.jpg"; //declare the specific pagename of get
                 HttpRequester req = new HttpRequester();
                 req.request(host, resourceLoc); //send the request mentiong the host and pagename
  
  
     }
    }
  
  class HttpRequester{
      public void request(String host, String resourceLoc) throws IOException{
           Socket httpSocket = new Socket(host, 80); //create the request for port 80
           PrintWriter writer = new PrintWriter(httpSocket.getOutputStream());
           FileOutputStream foutStream = new FileOutputStream("E:\\quora.jpeg"); //creating file to hold the output stream
  
           // building the header fields
          String protocol = "GET /" +resourceLoc+" HTTP/1.1";
          String connection ="Connection: close";
          String acceptedLanguage ="Accept-Language: de,en;q=0.7,en-us;q=0.3";
          String headerEnd = "";
          String HostHeader = "Host: www.uni-koblenz-landau.de";
  
          // writing the headers to the outputstream
  
         writer.println(protocol);
         writer.println(HostHeader);
         writer.println(connection);
         writer.println(acceptedLanguage);
         writer.println(headerEnd);
  
         writer.flush();
  
        // request sent
  
         BufferedInputStream reader = new BufferedInputStream(httpSocket.getInputStream());
  
         InputStream is;
  
         int byteCode =0;
         char ch ;
             StringBuilder builder = new StringBuilder();
  
         while((byteCode=reader.read())!=-1)
         {
             builder.append((char)byteCode);
            // System.out.print((char)byteCode);
  
         }
  
         String text = builder.toString();
         // sub[0] is supposed to contain the header and sub[1] should contain the bytes of the           image
  
         String[] sub = text.split("\r\n\r\n");
         System.out.println(sub[0]);
  
         byte[] byts = sub[1].getBytes();
  
         for(int i=0;i&lt;byts.length;i++){
             foutStream.write(byteCode);
         }
         System.out.println(byts.length);
      }
  
  }  
  </code></pre>