Google App Engine异步图片上传

时间:2014-12-11 09:20:21

标签: java javascript angularjs google-app-engine

我正在尝试使用异步POST将图像上传到Google App引擎。问题是我只能上传一张图片。之后我得到了一个内存不足的错误。我正在使用角度文件上传插件进行上传。我的代码如下

我用这个方法创建BLOB上传URLS

  public @ResponseBody String getBlobstoreUploadUrl() {
       BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
       return blobstoreService.createUploadUrl("/uploadquestionimage");
  }

Blob上传的方法是

 public @ResponseBody Map<String, List<BlobKey>> uploadQuestionImage(HttpServletRequest req) throws IOException {
    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
    return blobs;
}

我的Angular / Javascript代码是

$scope.fileSelected = function(a,b){
    console.log(a);
    var uploadurl='';
    $http({
        url : '/getImageUploadURL',
        method :'GET',
    }).success(function(url){
        uploadurl = url,
        console.log(url);
        $scope.upload = $upload.upload({
            url : uploadurl,
            method: 'POST',
            file: a
        }).success(function(dto){
            console.log(dto);
        }).error(function(err){
            console.log(err);
        })
    })

}

我收到错误

/_ah/upload/agx0ZXN0YnVkaGFlbnRyIgsSFV9fQmxvYlVwbG9hZFNlc3Npb25fXxiAgICAgODyCgw java.lang.OutOfMemoryError: Java heap space

2 个答案:

答案 0 :(得分:0)

你可以使用这样的代码..

BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

<form name="uploadImage" action="<%=blobstoreService.createUploadUrl("/UploadImage"
                        method="post" enctype="multipart/form-data">
 <input type="file" style="width: 250px" name="image">

在控制器中你可以获得blob和url。

有关详情,请参阅:http://code.google.com/appengine/docs/java/blobstore/overview.html

答案 1 :(得分:0)

我明白了。我使用angularUpload来模拟表单并使用getter来生成我需要的blob url。非常感谢