我使用谷歌云存储,我想知道我的文件上传是否成功 我使用可恢复的上传。
Google doc:google doc link
我可以使用会话网址上传文件:
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));
}
}
}
}
谷歌发送给我200,我的文件已被发送 谷歌的回应:
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
然后我想用一个请求检查这个 我提出这样的请求:(来自谷歌云doc)
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
我不明白。
谷歌说我将Content-Length设置为0并发送给我:
所需长度