2017-09-05 18 views
0

REST API v3を使用してGoogleドライブに256 KBのチャンクをアップロードしようとしています。私は正常にアップロードIDを得ることができますが、このアップロードIDを使用してチャンクをアップロードすると、308ではなく400のBad Requestが取得されます。以下のコードを投稿しています。メソッドgetUploadID()は再開可能なアップロードセッションを開始し、メソッドputFileWithUploadID()はファイルのチャンクをアップロードする必要がありますが、そこに問題があるようです。私は公式guideに従ってコードを書いています。GoogleドライブREST API再開可能なアップロードreturnin 400不正なリクエスト

String mUploadID; 
private String getUploadID(Uri fileUri, String token) { 
    String upload_id = ""; 
    java.io.File fileContent = new java.io.File(fileUri.getPath()); 
    String fileName = fileContent.getName(); 
    String mimeType = "audio/mpeg"; 
    try { 
     String url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable"; 
     URL obj = new URL(url); 
     HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); 
     con.setRequestMethod("POST"); 
     con.setDoInput(true); 
     con.setDoOutput(true); 
     con.setRequestProperty("Authorization", "Bearer " + token); 
     con.setRequestProperty("X-Upload-Content-Type", mimeType); 
     con.setRequestProperty("X-Upload-Content-Length", String.format(Locale.ENGLISH, "%d", fileContent.length())); 
     con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
     String body = "{\"name\": \"" + fileName + "\", \"parents\": [\"" + "0B7ypsm4HGZhCS3FXcldPZnFPNkE" + "\"]}"; 
     con.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", body.getBytes().length)); 
     OutputStream outputStream = con.getOutputStream(); 
     outputStream.write(body.getBytes()); 
     outputStream.close(); 
     con.connect(); 
     String location = con.getHeaderField("Location"); 
     if (location.contains("upload_id")) { 
      String[] uploadParameters = location.split("upload_id"); 
      upload_id = uploadParameters[1].replace("=", ""); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return upload_id; 
} 

private void putFileWithUploadID(Uri fileUri, String token, long range) { 
    java.io.File fileContent = new java.io.File(fileUri.getPath()); 
    String fileName = fileContent.getName(); 
    String contentLength = String.valueOf(fileContent.length()); 
    String mimeType = "audio/mpeg"; 
    long totalBytesFromDataInputStream = 0; 
    long uploadedBytes = 0; 
    long chunkStart = 0; 
    long chunkSize = 262144; 
    do { 
     try { 
      String url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=" + mUploadID; 
      URL obj = new URL(url); 
      HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); 
      con.setRequestMethod("PUT"); 
      con.setDoOutput(true); 
      con.setConnectTimeout(10000); 
      con.setRequestProperty("Content-Type", mimeType); 
      uploadedBytes = chunkSize; 
      if (chunkStart + uploadedBytes > fileContent.length()) { 
       uploadedBytes = (int) fileContent.length() - chunkStart; 
      } 
      con.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", uploadedBytes)); 
      con.setRequestProperty("Content-Range", "bytes " + chunkStart + "-" + (chunkStart + uploadedBytes - 1) + "/" + fileContent.length()); 
      byte[] buffer = new byte[(int) uploadedBytes]; 
      FileInputStream fileInputStream = new FileInputStream(fileContent); 
      fileInputStream.getChannel().position(chunkStart); 
      if (fileInputStream.read(buffer, 0, (int) uploadedBytes) == -1) { 
       break; 
      } 
      fileInputStream.close(); 
      OutputStream outputStream = con.getOutputStream(); 
      outputStream.write(buffer); 
      outputStream.close(); 
      con.connect(); 
      int responseCode = con.getResponseCode(); 
      String rangeHeader = con.getHeaderField("Range"); 
      if (rangeHeader!=null) { 
       chunkStart = Long.parseLong(rangeHeader.substring(rangeHeader.lastIndexOf("-") + 1, rangeHeader.length())) + 1; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } while ((chunkStart+chunkSize)<fileContent.length()); 
} 

答えて

0

基本的に、400: Bad Requestは必須フィールドまたはパラメータが提供されていない、指定された値が無効である、または提供されたフィールドの組み合わせが無効であることを意味します。

このエラーは、複製アイテムをドライブアイテムに追加しようとするとスローされる可能性があります。また、ディレクトリグラフにサイクルを作成する親を追加しようとするとスローされます。

また、related SO postには、Android固有のAPIを正しく使用して再開可能なアップロードを行うことをお勧めします。 creating filesを参照してください。

+0

AndroidのAPIを使用していないため、アップロードの進捗状況を確認することができず、上のコードでは何かを提供していないか、無効なものを提供していません。 – Ali

関連する問題