当前位置: 首页 > 工具软件 > picasa albums > 使用案例 >

java取picasa web相册

杨柏
2023-12-01
PicasaTestAction:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.Link;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.GphotoEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.UserFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import com.opensymphony.xwork2.ActionSupport;
import com.picasa.storage.PhotoInfo;

public class PicasaTestAction extends ActionSupport
{
private static final long serialVersionUID = -3709074572710209488L;

private static final String API_PREFIX = "http://picasaweb.google.com/data/feed/api/user/";

private static final String RESULT = "RESULT";
private static final String FAIL = "FAIL";

/**
* 取picasa相册
*/
public String execute() throws ServletException, IOException
{
HttpServletRequest request = ServletActionContext.getRequest();

String emailAddress = request.getParameter("emailAddress");
String passWord = request.getParameter("password");
String realPath = request.getRealPath("/image");

if (null == emailAddress || null == passWord)
{
return FAIL;
}

Map<String, List<PhotoInfo>> albumPhotos = generatePhoto(emailAddress, passWord, realPath);
request.setAttribute("albumPhotos", albumPhotos);
return RESULT;
}

/**
* 返回所有的相册和相片的信息
* @param emailAddress
* @param passWord
* @param realPath
* @return
* @throws IOException
*/
public Map<String, List<PhotoInfo>> generatePhoto(String emailAddress,
String passWord, String realPath) throws IOException
{
try
{
PicasawebService picasaService = buildPicasaWebService("Picasa", emailAddress, passWord);
List<AlbumEntry> albums = getAlbums(picasaService, emailAddress);
Map<String, List<PhotoInfo>> albumPhotos = new Hashtable<String, List<PhotoInfo>>();
for (AlbumEntry albumEntry : albums) //相册
{
List<PhotoEntry> photoEntrys = getPhotos(picasaService, albumEntry);
if (photoEntrys.size() > 0)
{
List<PhotoInfo> photos = new ArrayList<PhotoInfo>();
for (PhotoEntry photoEntry : photoEntrys) //相片
{
//原图
PhotoInfo photoInfo = new PhotoInfo();

String originalName = writeImage(photoEntry
.getMediaContents().get(0).getUrl(), realPath, true);

photoInfo.setAlbumName(albumEntry.getTitle().getPlainText());

photoInfo.setPhotoName(originalName);

photoInfo.setPhotoDesc(photoEntry.getDescription().getPlainText());

photos.add(photoInfo);
}
albumPhotos.put(albumEntry.getTitle().getPlainText(), photos);
}
}
return albumPhotos;
}
catch (ServiceException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return new Hashtable<String, List<PhotoInfo>>();
}

/**
* 取得相册
* @param picasaService
* @param username
* @return
* @throws IOException
* @throws ServiceException
*/
@SuppressWarnings("unchecked")
public List<AlbumEntry> getAlbums(PicasawebService picasaService,
String username) throws IOException, ServiceException
{

String albumUrl = API_PREFIX + username;

UserFeed userFeed = picasaService.getFeed(new URL(albumUrl),
UserFeed.class);

List<GphotoEntry> entries = userFeed.getEntries();

List<AlbumEntry> albums = new ArrayList<AlbumEntry>();
for (GphotoEntry entry : entries)
{
GphotoEntry adapted = entry.getAdaptedEntry();
if (adapted instanceof AlbumEntry)
{
albums.add((AlbumEntry) adapted);
}
}
return albums;
}

/**
* 取得相片
* @param picasaService
* @param album
* @return
* @throws IOException
* @throws ServiceException
*/
@SuppressWarnings("unchecked")
public List<PhotoEntry> getPhotos(PicasawebService picasaService,
AlbumEntry album) throws IOException, ServiceException
{

String feedHref = getLinkByRel(album.getLinks(), Link.Rel.FEED);
AlbumFeed albumFeed = picasaService.getFeed(new URL(feedHref),
AlbumFeed.class);

List<GphotoEntry> entries = albumFeed.getEntries();
List<PhotoEntry> photos = new ArrayList<PhotoEntry>();
for (GphotoEntry entry : entries)
{
GphotoEntry adapted = entry.getAdaptedEntry();
if (adapted instanceof PhotoEntry)
{
photos.add((PhotoEntry) adapted);
}
}
return photos;
}

/**
* 取得相册的连接
* @param links
* @param relValue
* @return
*/
public String getLinkByRel(List<Link> links, String relValue)
{
String linkStr = null;
for (Link link : links)
{
if (relValue.equals(link.getRel()))
{
linkStr = link.getHref();
}
}
return linkStr;
}

/**
* 把相片保存到本地
* @param urlAddress
* @param realPath
* @param isThumbnail
* @return
*/
public String writeImage(String urlAddress, String realPath,
boolean isThumbnail)
{
BufferedInputStream bis = null;
OutputStream bos = null;
String fileName = null;
try
{
URL url = new URL(urlAddress);
bis = new BufferedInputStream(url.openStream());
fileName = getFileName(urlAddress);
byte[] bytes = new byte[1024];
File file = new File(realPath + "\\" + fileName);
if (!file.isFile()) //如果已经存在直接返回该文件名
{
bos = new FileOutputStream(file);
int len;
while ((len = bis.read(bytes)) > 0)
{
bos.write(bytes, 0, len);
}
}
return fileName;
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (null != bos)
{
bos.flush();
bos.close();
}
if (null != bis)
{
bis.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
return null;
}

/**
* 取得相片的文件名
* @param urlAddress
* @return
*/
public String getFileName(String urlAddress)
{
int lastURLSeparater = urlAddress.lastIndexOf("/");
return urlAddress.substring(lastURLSeparater + 1);

}

/**
* 通过appName,emailAddress,passWord返回PicasawebService对象
* @param appName
* @param emailAddress
* @param password
* @return
* @throws AuthenticationException
*/
public PicasawebService buildPicasaWebService(String appName,
String emailAddress, String passWord)
throws AuthenticationException
{
PicasawebService picasaService = new PicasawebService(appName);
picasaService.setUserCredentials(emailAddress, passWord);
return picasaService;
}
}


PhotoInfo:

public class PhotoInfo
{
/*
* 相片所属的相册
*/
private String albumName;

/*
* 相片的名称
*/
private String photoName;

/*
* 相片的描述
*/
private String photoDesc;

/*
* 相片的个数
*/
private String photoNumber;

/**
* getter方法
*/
public String getAlbumName()
{
return albumName;
}

/**
* setter方法
*/
public void setAlbumName(String albumName)
{
this.albumName = albumName;
}

/**
* getter方法
*/
public String getPhotoName()
{
return photoName;
}

/**
* setter方法
*/
public void setPhotoName(String photoName)
{
this.photoName = photoName;
}

/**
* getter方法
*/
public String getPhotoDesc()
{
return photoDesc;
}

/**
* setter方法
*/
public void setPhotoDesc(String photoDesc)
{
this.photoDesc = photoDesc;
}

/**
* getter方法
*/
public String getPhotoNumber()
{
return photoNumber;
}

/**
* setter方法
*/
public void setPhotoNumber(String photoNumber)
{
this.photoNumber = photoNumber;
}
}
 类似资料: