【发布时间】:2015-01-13 14:27:28
【问题描述】:
我正在使用以下代码将图像上传到我的谷歌云存储中的存储桶:
File file = new File("test.jpg");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://www.googleapis.com/upload/storage/v1/b/imagecachebucket/o?uploadType=media&name=test.jpg&projection=full");
httppost.setHeader("Content-Type", "image/jpeg");
FileBody fileB = new FileBody(file, "image/jpeg");
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("file", fileB);
httppost.setEntity(multipartEntity.build());
System.out.println( "executing request " + httppost.getRequestLine( ) );
try {
HttpResponse response = httpclient.execute( httppost );
System.out.println("response: " + response.getStatusLine().toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpclient.getConnectionManager( ).shutdown( );
图片已上传,我可以在云存储浏览器中看到它,但是当我想查看图片时,它坏了,只有标准图标无法查看。当我通过云存储浏览器上传图像时,图像已正确上传。
【问题讨论】:
-
嗨 - 我没有使用您正在使用的 Apache lib,但有一些问题/建议:(a) 对象大小(来自 Cloud Storage 浏览器或 gsutil)是否与你上传的文件? (b) 如果你使用不同的 HttpMultipartMode,比如 STRICT,会发生什么?
-
大小一样。使用 STRICT 也是同样的问题。
标签: java image google-cloud-storage