---
title: "GAE上传图片,新增大对象数据的问题"
author: "jerryllx"
date: "2009-08-10T20:37:25Z"
tags: ["GAE", "Google", "HTML", "Web"]
slug: "gae"
draft: false
lastmod: "2009-08-12T11:43:48Z"
id: 444057
url: "https://jerryllx.iteye.com/blog/444057"
---

前一段时间公司做了一个GAE的Demo,Demo地址在
<http://www.easygae.com/Product/view/id/ag5kYXRvbmdzb2Z0d2FyZXINCxIHUHJvZHVjdBgHDA.html>
如果想了解GAE的请到下面的网站查看
<http://code.google.com/intl/zh-CN/appengine/docs/whatisgoogleappengine.html>
里面有相关的介绍,就不多说了, 下面就将碰到的问题讲讲吧
GAE上传图片时要调用Google的Picasa Web Albums Data API,请参考下面的网站
<http://code.google.com/apis/gdata/>
Google的数据处理的API都在里面有介绍.
下面将上传图片的步骤讲一下:
1.申请一个http://picasaweb.google.com相簿ID(albumid),并获取用户名
2.下载Google Data的JAR,下载地址:
<http://code.google.com/p/gdata-java-client/downloads/list>

上传图片时假定已经申请了Google相簿,如果没有请到http://picasaweb.google.com申请相簿,并记录相簿ID.
下面将代码贴出来看一下,我用的是Serlvet上传,上传组件是commons-fileupload-1.2.1.jar
PicasawebService picasawebService = new PicasawebService("easygae");
            String username = "";//用户名
            String passwd = ""; //密码
            String albumid="";  //相簿ID
            picasawebService.setUserCredentials(username, passwd);
            String oldPhotoid = photoInfo.getPhotoid();
            PhotoEntry oldEntry = (PhotoEntry)picasawebService.getEntry(new URL(oldPhotoid), PhotoEntry.class);
            //先刪除圖片，然後新增
            if(oldEntry!=null)
                oldEntry.delete();
            //相簿地址
            String albumPostUrl = "http://picasaweb.google.com/data/feed/api/user/"+username+"/albumid/"+albumid;           
            PhotoEntry photoEntry = new PhotoEntry();
            photoEntry.setTitle(new PlainTextConstruct(photoInfo.getTitle()));
            photoEntry.setDescription(new PlainTextConstruct(
                    photoInfo.getDescription()));
            photoEntry.setClient(applicationName);
            photoEntry.setContent(photoInfo.getOtherContent());
             // 上传图片
            PhotoEntry returnedPhoto = picasawebService.insert(new URL(albumPostUrl),
                    photoEntry);
            MediaContent content = (MediaContent)returnedPhoto.getContent();
            photoInfo.setPicURL(content.getUri()) ;
            photoInfo.setPhotoid(returnedPhoto.getId());

另外GAE新增大数据时,我Model的栏位类型是String,心想String的最大长度是4G,保存大字段时应该没问题,谁知道超过了500字时就自动截取了,後來用
	@Persistent
	private com.google.appengine.api.datastore.Text siteContent ;
就可以保存进去了.