【问题标题】:Google Cloud Storage: Uploaded image is corruptGoogle Cloud Storage:上传的图片已损坏
【发布时间】: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


【解决方案1】:

看起来您正在执行仅包含 1 个部分的分段上传,但您已将 uploadType 指定为“媒体”。

媒体上传类型适用于您只是上传文件的情况。在这种情况下,Google Cloud Storage 期望整个正文都是正在上传的对象。

如果您想进行分段上传,那很好。为此,您应该使用上传类型“multipart”。分段上传需要两部分,第一部分是对象的元数据(权限、自定义用户元数据等),第二部分是数据。

这里有每种上传类型的确切文档:https://developers.google.com/api-client-library/php/guide/media_upload

我的 HttpClient-fu 不是很好,但我认为“媒体”案例看起来更像这样:

FileEntity entity = new FileEntity(file, 
ContentType.create("text/plain", "UTF-8"));        
HttpPost httppost = new HttpPost("https://www.googleapis.com/upload/storage/v1/b/imagecachebucket/o?uploadType=media&name=test.jpg&projection=full");
httppost.setEntity(entity);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 2014-10-10
    • 2020-04-08
    • 2020-07-26
    • 2019-05-18
    • 1970-01-01
    • 2015-02-21
    相关资源
    最近更新 更多