2017-09-05 58 views
0

我試圖使用REST API v3在Google Drive中上傳256 KB的塊。我可以成功獲取上傳ID,但是當我使用此上傳ID上傳一個塊時,我得到了400錯誤請求,而不是308。我在下面發佈代碼。方法getUploadID()啓動一個可恢復的上傳會話,putFileWithUploadID()方法應該上傳一個文件塊,但似乎存在問題。我已經根據官方guide編寫了代碼。Google Drive 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