【问题标题】:How to upload an image to BlobStore using MediaHttpUploader?如何使用 MediaHttpUploader 将图像上传到 BlobStore?
【发布时间】:2012-07-08 15:20:10
【问题描述】:

我在 android 中使用以下函数,但在调用 uploader.upload() 时出现错误“500 Java Heap Space”。

如果我在uploader.upload() 之前调用uploader.setDirectUploadEnabled(true),我会在upload() 上收到错误“411 Length required”而不是错误 500。

public static String uploadImage(Context context, String file, MediaHttpUploaderProgressListener progressListener)
            throws Exception {
// The file is something like : /mnt/sdcard/wallpapers/image.jpg
        File mediaFile = new File(file);
        HttpTransport transport = new NetHttpTransport();       

        HttpRequestFactory factory = transport.createRequestFactory(null);
//obtain the upload url
        GenericUrl getUploadUrl = new GenericUrl(Util.getBaseUrl(context)
                + "/myapp/blobservice?get-key");
        HttpRequest request = factory.buildGetRequest(getUploadUrl);
        HttpResponse response1 = request.execute();

        String postUrl = convertStreamToString(response1.getContent()).trim();
// the returned post url when using the development server is something like : //http://192.168.1.4:8888/_ah/upload/agd5YWRnZXQychwLEhVfX0Jsb2JVcGxvYWRTZXNzaW9uX18Y2wUM

        InputStreamContent mediaContent = new InputStreamContent("image/jpeg",
                new BufferedInputStream(new FileInputStream(mediaFile)));
        mediaContent.setLength(mediaFile.length());
        MediaHttpUploader uploader = new MediaHttpUploader(mediaContent,transport, null);

        uploader.setProgressListener(progressListener);

//the following line produces exception "500 Java heap space" on the local server
//on the remote server I get "500: Internal server error"

        HttpResponse response = uploader.upload(new GenericUrl(postUrl));
        if (!response.isSuccessStatusCode()) {
            throw new Exception("Uploading image failed.");
        } else {
            String blobKey = convertStreamToString(response.getContent()).trim();
            return blobKey;
        }
    }

【问题讨论】:

  • 由于错误发生在服务器端,您应该提供日志中的错误条目和产生错误的服务器代码。
  • 也许这会有所帮助:stackoverflow.com/questions/15704816/… 虽然它没有用 MediaHttpUpload 回答你的问题,但它显示了一个使用 Resteasy 轻松上传图像的工作代码

标签: android google-app-engine google-api google-api-java-client


【解决方案1】:

好像MediaHttpUpload使用resumable protocol,与multipart/form-data file upload of GAE blobstore不兼容。

您应该创建一个custom data upload servlet,它知道如何处理resumable protocol

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    相关资源
    最近更新 更多