title: How to upload a video file via HTTP POST in JAVA without using youtube api
body: <p>I want to upload some video file to my Youtube. </p>
  
  <p>I know the official site (<a href="https://developers.google.com/youtube/v3/docs/videos" rel="nofollow noreferrer">https://developers.google.com/youtube/v3/docs/videos</a>) but I'm using android platform but there is no android play services. </p>
  
  <p>And then I know about Google API Client Library. </p>
  
  <p>I look up API samples
  (<a href="https://github.com/youtube/api-samples/blob/master/java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/UploadVideo.java" rel="nofollow noreferrer">https://github.com/youtube/api-samples/blob/master/java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/UploadVideo.java</a>). </p>
  
  <p>The sample use <code>com.google.api.services.youtube</code>. but i can't use the YouTube APIs. </p>
  
  <p>I just upload a video file via http in java without youtube api. </p>
  
  <p>This is my test source. 
  Annotation (// this is not working) is sending vedio. If the source Uncommented, I received 400 responses. If the source Comment, I received 200 responses. </p>
  
  <pre><code>public static String uploadMetaData(String filePath, boolean retry, String accessToken) throws IOException {
  
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary = "*****";
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1 * 1024 * 1024;
  
      //String baseURL = "https://www.googleapis.com/youtube/v3/videos";
      String baseURL = "https://www.googleapis.com/upload/youtube/v3/videos";
      String urlData = "uploadType=resumable&amp;part=snippet,status,contentDetails";
      String uploadUrl = baseURL + "?" + urlData;
  
  
      String postDataForm = "{\n\"snippet\": {\n\"title\": \"%s\",\n\"description\": \"%s\",\n\"tags\": " + "[\"cool\", \"video\", \"more keywords\"],\n\"categoryId\": 22\n},\n\"status\": " + "{\n\"privacyStatus\": \"public\",\n\"embeddable\": True,\n\"license\": \"youtube\"\n}\n}";
      String postData = String.format(postDataForm, "hong", "hong", "hong");
      JSONObject jsonObj = null;
      try {
          jsonObj = new JSONObject(postData);
      } catch (JSONException e) {
          e.printStackTrace();
      }
  
      URL url = new URL(uploadUrl);
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod("POST");
      urlConnection.setRequestProperty("Host", "www.googleapis.com");
      urlConnection.setRequestProperty("Authorization", String.format("Bearer %s", accessToken));
      urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
      urlConnection.setRequestProperty("X-Upload-Content-Type", "video/*");
  
      //urlConnection.setRequestProperty("Content-Length", String.valueOf(postData.length()));
      //urlConnection.setRequestProperty("X-Upload-Content-Length", );
      urlConnection.setDoOutput(true);
  
      String uploadFileName = "1541437905497.mp4";
      String fileName = Environment.getExternalStorageDirectory().getPath() + "/cmxMediaGallery/1541437905497.mp4";
  
  
      FileInputStream fileInputStream = new FileInputStream(fileName);
  
      DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream());
  
      dos.writeBytes(jsonObj.toString());
  
      // this is not working
      /*dos.writeBytes(twoHyphens + boundary + lineEnd);
      dos.writeBytes("Content-Disposition: form-data; name=" + uploadFileName + ";filename="
              + fileName  + lineEnd);
  
              dos.writeBytes(lineEnd);
  
      // create a buffer of  maximum size
      bytesAvailable = fileInputStream.available();
  
      bufferSize = Math.min(bytesAvailable, maxBufferSize);
      buffer = new byte[bufferSize];
  
      bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  
      while (bytesRead &gt; 0) {
  
          dos.write(buffer, 0, bufferSize);
          bytesAvailable = fileInputStream.available();
          bufferSize = Math.min(bytesAvailable, maxBufferSize);
          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  
      }*/
  
      dos.flush();
      dos.close();
  
      int responseCode = urlConnection.getResponseCode();
      if (responseCode &lt; 200 || responseCode &gt;= 300) {
          if ((responseCode + "").startsWith("4") &amp;&amp; retry) {
              return uploadMetaData(filePath, false, accessToken);
          } else {
              throw new IOException(String.format("response code='%s' (code %d)" + " for %s",
                      urlConnection.getResponseMessage(), responseCode, urlConnection.getURL()));
          }
      }
  
      Log.d("Hong", "responseCode : " + responseCode);
  
      return urlConnection.getHeaderField("Location");
  }
  </code></pre>
  
  <p>Some example use multipart. but it's working. 
  <code>connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);</code></p>
  
  <p>I wanna know how to upload a video file via HTTP POST in JAVA without using youtube api.</p>