title: How can get image from http response in java?
body: <p>I want to get an image from a server by making http request manually . I make a connection , create a http request wait for , get the http reply from server and then i want data inside it.I separated header from data and saved it into a file with ".jpeg" extension . But my .jpeg file could not be opened. What can i do ?</p>
  
  <p>Note: i cant use any plugin or libraries . I just can process on http request!  </p>
  
  <p>Here my code :</p>
  
  <pre><code>try (Socket socket = new Socket("ceit.aut.ac.ir", 80)) {
  
          // send an HTTP request to the web server
          DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream());
          BufferedReader inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          outToServer.writeBytes("GET /~94131090/CN1_Project_Files/flower.jpeg  HTTP/1.1\r\n");
          outToServer.writeBytes("Host: ceit.aut.ac.ir:80\r\n");
          outToServer.writeBytes("Connection: Close\r\n");
          outToServer.writeBytes("Content-Type:image/*\r\n");
          outToServer.writeBytes("\r\n");
          // Receive an HTTP reply from the web server
          boolean loop = true;
          StringBuilder sb = new StringBuilder();
          while (loop) {
              if (inFromServer.ready()) {
                  int i = 0;
                  while (i != -1) {
  
  
                      i = inFromServer.read();
                      sb.append((char) i);
                  }
                  loop = false;
              }
          }
  
          //Download Image
          String data = separate(sb.toString());
          //???
  
          socket.close();
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>Here my separate function : </p>
  
  <pre><code>public String separate(String str){
      String msg = str;
  
      int index = msg.indexOf("close");
  
      // "close" and blank end of http response line including \r\n
      // 3(close) + 2(\r) + 2 (\n) + 2 (\r) + 2 (\n) = 4 + 2 + 2 + 2 = 6
  
      return msg.substring(index+11);
  
  }
  </code></pre>