google drive rest api可恢复上传返回400个错误请求

zu0ti5jz  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(438)

我正在尝试使用restapiv3在googledrive上传256kb的数据块。我可以成功地获得上传id,但是当我使用这个上传id上传一个块时,我得到了一个400错误的请求,而不是308。我把代码贴在下面。方法getuploadid()启动一个可恢复的上载会话,方法putfilewithuploadid()应该上载一个文件块,但其中似乎有问题。我已经根据官方指南写了代码。

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());
}
4ngedf3f

4ngedf3f1#

基本上,400:错误的请求意味着没有提供必需的字段或参数,提供的值无效,或者提供的字段组合无效。
尝试将重复的父级添加到驱动器项时,可能会引发此错误。当试图添加将在目录图中创建循环的父级时,也可以抛出它。
你也可以查看这个相关的so帖子,它建议正确使用android特定的api来进行可恢复的上传。请参见创建文件。

相关问题