如何使用MediaHttpUploader将图像上传到BlobStore



我在android中使用了以下函数,但在调用uploader.upload()时出现错误"500 Java堆空间"。

如果我在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;
        }
    }

似乎MediaHttpUpload使用了可恢复协议,与GAE blobstore的multipart/form-data文件上传不兼容。

您应该创建一个自定义的数据上传servlet,它知道如何处理可恢复的协议。

相关内容

  • 没有找到相关文章

最新更新