将图像上传到谷歌应用引擎上的blob商店

时间:2014-04-02 12:34:37

标签: java android google-app-engine


我是应用引擎的新手,并且正致力于在blob商店上传图片。我从android应用程序上传图像。我计划在实体中保存blob密钥,以便在符合特定条件时可以检索它。

我的问题是:
自从我实施了#34;上传处理程序" blob商店不存储图像。在实施"上传处理程序"它正确地保存了它 2.另一个问题是,上传处理程序没有获得任何blob密钥。

请找到以下代码:

对于上传图片,以下代码用于Android:

public void uploadBlobImage( String path, String link ) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(link);

        try {
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("type", new StringBody("photo"));
            File file = new File( path );
            entity.addPart("data", new FileBody(file, ContentType.create( "image/jpg")/*,"Swapnil.jpg" */));

            httppost.setEntity(entity);
            HttpResponse response = httpclient.execute(httppost);
            Log.d( Utils.AppName, "Image uploaded: "+ response.getStatusLine( ));
        } catch (Exception e) {
            Log.d( Utils.AppName, "Image not uploaded: Exception:"+ e.toString( ));
        }        
    }

上传链接的格式为:

http://<server domain>/_ah/upload/AMmfu6YsZrgX5lDxBjlb5ddPQdGU6kd63WD9nN8FI2rOGpC1VA9qoyYneKYiLvNGNR0Mw-Oi_LyfKzxI9lvL7rjk0CoTuN7O_1rRCmTKLB2c4MOjRWJ8BKuiaquIvcQf-1H47ULHbMoY6w5_lZts8JiG1DDPez8iAQ/ALBNUaYAAAAAUzv_0cB5QIh3hkGn4CokBt0a37zGDAGm/

上传blob处理程序代码为:

public class UploadBlobHandler extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
        BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

        Map<String, List<BlobKey>> blobs = blobstoreService.getUploads( req );
        Set< String > set = blobs.keySet( );
        Utils.log.log( Level.SEVERE, "UploadBlobHandler.doPost() size::"+set.size( ));
        Iterator< String > iterator = set.iterator( );
        while ( iterator.hasNext( )) {
            Utils.log.log( Level.SEVERE, "UploadBlobHandler.doPost(): Blobstore key: "+iterator.next( ));
        }
        List< BlobKey> list = blobs.get("data");
        for(int i=0;list !=null && i<list.size( );i++){
            BlobKey key = list.get( i );
            Utils.log.log( Level.SEVERE, "UploadBlobHandler.doPost(): Blobstore key: "+key.getKeyString( ));
        }
}

我尝试在web.xml中添加和删除以下条目:

<servlet>
        <servlet-name>UploadBlobHandler</servlet-name>
        <servlet-class>com.swapnilutilities.eRecommendation.servlets.UploadBlobHandler</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UploadBlobHandler</servlet-name>
        <url-pattern>/upload</url-pattern>
    </servlet-mapping>

在Android上生成以下日志:

Image uploaded: HTTP/1.1 200 OK

在服务器日志上是:

0.1.0.30 - - [02/Apr/2014:05:02:02 -0700] "POST /upload HTTP/1.1" 200 53 - "Apache-HttpClient/UNAVAILABLE (java 1.4)" "domain.com" ms=327 cpu_ms=260 cpm_usd=0.000006 app_engine_release=1.9.2 instance=00c61b117cbb73996c2a0799c02bf971906801e2
E 2014-04-02 17:32:02.764
package.UploadBlobHandler doPost: UploadBlobHandler.doPost() size::0
E 2014-04-02 17:32:02.765
package.utils.Utils writeResponse: Swapnil: writeResponse():{"status":0,"ec":0}

请指导我。

1 个答案:

答案 0 :(得分:0)

问题解决了。虽然我不明白为什么会这样。 我改变了

new FileBody(file, ContentType.create( "image/jpg")/*,"Swapnil.jpg"*/ )

new FileBody(file, ContentType.create( "image/jpg"),"Swapnil.jpg" ).

出于某种原因,它解决了这个问题。