Google cloud storage 谷歌云存储:所需长度411

Google cloud storage 谷歌云存储:所需长度411,google-cloud-storage,http-status-code-411,Google Cloud Storage,Http Status Code 411,我使用谷歌云存储,我想知道我的文件上传是否成功。 我使用可恢复上传 谷歌文档: 我可以使用会话url上载文件: byte[] byteArray = Files.readAllBytes(path); long byteCount = byteArray.length; //UrlForUp is my session_uri for resumable upload String urlForUp = ObjectManager.getUrl(bucketName, objectName,

我使用谷歌云存储,我想知道我的文件上传是否成功。
我使用可恢复上传

谷歌文档:

我可以使用会话url上载文件:

byte[] byteArray = Files.readAllBytes(path);

long byteCount = byteArray.length;

//UrlForUp is my session_uri for resumable upload
String urlForUp = ObjectManager.getUrl(bucketName, objectName, properties, "image/gif", byteCount);


HttpURLConnection connection;
URL url = new URL(urlForUp);

connection = (HttpURLConnection) url.openConnection();

connection.setDoOutput(true);
connection.setRequestMethod("PUT");

connection.setRequestProperty("Content-Length", Long.toString(byteCount));

connection.connect();

OutputStream os = connection.getOutputStream();

//send file
os.write(byteArray);
os.flush();
os.close();

Map<String, List<String>> headerData;

headerData = connection.getHeaderFields();

Set listKeys = headerData.keySet();
Iterator iterator = listKeys.iterator();

System.out.println("UPLOAD RESPONSE CODE---------------------------------");
System.out.println(connection.getResponseCode());

System.out.println("UPLOAD RESPONSE HEADER---------------------------------");
while (iterator.hasNext()) {
    Object key = iterator.next();
    if (key != null) {
        List<String> values = headerData.get(key);
        for (int i = 0; i < values.size(); i++) {
            if (values.get(i) != null) {
                System.out.println(key.toString() + " : " + values.get(i));
            }
        }
    }
}
然后我想通过一个请求来检查这一点。
我提出这样的请求:(来自谷歌云文档)

我不明白。
谷歌让我将内容长度设置为0并发送给我:

所需长度

您的PUT请求试图做什么?还有,您使用direct HTTP而不是?我使用这个PUT请求和我的令牌和文件大小来了解文件是否已完全上载。google api java客户端中是否有检查可恢复上传的功能?是的,java客户端为您处理可恢复上传。查看中关于
setDirectUploadEnabled
的注释。我可以看到
getObject.executemedia和downloadto(out)下载对象。是否存在其他解决方案?仅用于获取大小信息。是的,请参见上面示例中的getObjectMetadata。
UPLOAD RESPONSE CODE---------------------------------    
200    
UPLOAD RESPONSE HEADER---------------------------------    
ETag : CICQ7oauzcICEAE=    
Date : Wed, 17 Dec 2014 15:10:38 GMT    
Vary : X-Origin    
Vary : Origin    
Content-Length : 810    
Expires : Fri, 01 Jan 1990 00:00:00 GMT    
Alternate-Protocol : 443:quic,p=0.02    
Content-Type : application/json; charset=UTF-8    
Server : UploadServer ("Built on Dec 2 2014 12:42:30 (1417552950)")
Pragma : no-cache
Cache-Control : no-cache, no-store, max-age=0, must-revalidate
PUT {session_uri} HTTP/1.1
Authorization: Bearer your_auth_token
Content-Length: 0
Content-Range: bytes */2000000



HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();

connection.setDoOutput(true);
connection.setRequestMethod("PUT");

Credential credential = createCredential(properties);
credential.refreshToken();

connection.setRequestProperty("Authorization", "Bearer " + credential.getAccessToken());
connection.setRequestProperty("Content-Length", "0");
connection.setRequestProperty("Content-Range","bytes */"+fileSize);

connection.connect();

System.out.println("CONNECTION MESSAGE-----------------------------------------");
System.out.println(connection.getResponseMessage());


Map<String, List<String>> headerData;

headerData = connection.getHeaderFields();


Set listKeys = headerData.keySet();
Iterator iterator = listKeys.iterator();

while (iterator.hasNext()) {
    Object key = iterator.next();
    if (key != null) {
        List<String> values = headerData.get(key);
        for (int i = 0; i < values.size(); i++) {
            if (values.get(i) != null) {
                System.out.println(key.toString() + " : " + values.get(i));
            }
        }
    }
}
System.out.println(connection.getResponseCode());
CONNECTION MESSAGE-----------------------------------------
Length Required
Date : Thu, 18 Dec 2014 12:12:29 GMT
Content-Length : 1428
Content-Type : text/html; charset=UTF-8
Server : GFE/2.0
411