title: How to get the stream from the IP WebCam Android app in Java
body: <p>I try to make a Java program which can get images from a smartphone. I succeeded to connect to the webserver of the app with an URLConnection and to get some data but the problem is that when I write images to a file, the file seems corrupted when I open it with a program. I've seen that the data is in the MJPEG format, do I need to decode it with something or is this just a problem with the data retrieving ? </p>
  
  <p>Here's my code :</p>
  
  <pre><code>package fr.paragoumba.streamingdisplay;
  
  import java.io.*;
  import java.net.Authenticator;
  import java.net.PasswordAuthentication;
  import java.net.URL;
  
  public class Test {
  
      /**
       * @param args
       */
      public static void main(String[] args) {
          Test mp = new Test("http://192.168.1.19:8080/video", null, null);
      }
  
      public Test(String mjpeg_url, String username, String password)
      {
          int imageCount = 0;
  
          try {
              if (username != null &amp;&amp; password != null)
              {
                  Authenticator.setDefault(new HTTPAuthenticator(username, password));
              }
  
              File file = new File("D:/Desktop/Dev/Out/images/image" + System.currentTimeMillis() + ".jpg");
              boolean firstImage = true;
  
              if (!file.exists()) file.createNewFile();
  
              URL url = new URL(mjpeg_url);
              BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
              String inputLine;
              int lineCount = 0;
              boolean lineCountStart = false;
              boolean saveImage = false;
              while ((inputLine = in.readLine()) != null) {
  
                  //File image = new File("D:/Desktop/Dev/Out/images/image" + imageCount + ".jpeg");
  
                  // Should be checking just for "--" probably
                  if (inputLine.lastIndexOf("--Ba4oTvQMY8ew04N8dcnM") &gt; -1)
                  {
                      // Got an image boundary, stop last image
                      // Start counting lines to get past:
                      // Content-Type: image/jpeg
                      // Content-Length: 22517
  
                      saveImage = false;
                      lineCountStart = true;
  
                      System.out.println("Got a new boundary");
                      System.out.println(inputLine);
  
                      if (!firstImage) System.exit(0);
                  }
                  else if (lineCountStart)
                  {
                      lineCount++;
                      if (lineCount &gt;= 2)
                      {
                          lineCount = 0;
                          lineCountStart = false;
                          imageCount++;
                          saveImage = true;
                          System.out.println("Starting a new image");
  
                          firstImage = !firstImage;
  
                          //if (!image.exists()) image.createNewFile();
                      }
                  }
                  else if (saveImage)
                  {
                      System.out.println("Saving an image line: " + inputLine);
  
                      /*try(PrintWriter pw = new PrintWriter(file)){
  
                          pw.print(inputLine);
  
                      }*/
  
                      try(FileOutputStream fos = new FileOutputStream(file)){
                          fos.write(inputLine.getBytes());
                      }
  
                  }
                  else {
  
                      System.out.println("What's this:");
                      System.out.println(inputLine);
                  }
              }
              in.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
  
      static class HTTPAuthenticator extends Authenticator {
          private String username, password;
  
          public HTTPAuthenticator(String user, String pass) {
              username = user;
              password = pass;
          }
  
          protected PasswordAuthentication getPasswordAuthentication() {
              System.out.println("Requesting Host  : " + getRequestingHost());
              System.out.println("Requesting Port  : " + getRequestingPort());
              System.out.println("Requesting Prompt : " + getRequestingPrompt());
              System.out.println("Requesting Protocol: " + getRequestingProtocol());
              System.out.println("Requesting Scheme : " + getRequestingScheme());
              System.out.println("Requesting Site  : " + getRequestingSite());
              return new PasswordAuthentication(username, password.toCharArray());
          }
      }
  }
  </code></pre>
  
  <p>Thanks for the help</p>