`
shjie5246
  • 浏览: 10838 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
最近访客 更多访客>>
社区版块
存档分类
最新评论

java取picasa web相册

    博客分类:
  • java
阅读更多
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;
	}
}
分享到:
评论
3 楼 lwx123 2011-03-23  
PicasawebService myService = new PicasawebService("exampleCo-exampleApp-1");
myService.setUserCredentials("liz@gmail.com", "mypassword");
怎么在构造这里的时候报异常咧!


03-23 05:38:04.405: ERROR/dalvikvm(276): Could not find class 'com.google.gdata.data.media.MediaSource', referenced from method com.google.gdata.wireformats.input.media.MediaParser.parse
2 楼 shjie5246 2011-01-05  
不需要配置什么,只要把用到的第三方包放到所在的工程下就可以了
1 楼 ankang2577 2010-12-13  
我是一个java开发新手,楼主能给出如何在eclipse中配置Google Picasa API的流程吗

相关推荐

    picasaweb

    Google PicasaWeb(网络相册),很不错的

    picasa3相册-setup.exe

    最新最好用的picasa管理相册工具! picasa3相册-setup.exe

    picasaweb-current-setup.zip

    picasa 一款极棒的图片管理软件

    Java Picasaweb UI-开源

    一个Java Swing接口,公开了Picasa网络相册提供的功能。 允许您连接到Picasaweb帐户,查看/添加/删除相册,下载/删除/上传/预览照片,以及将照片从一个帐户复制到另一个帐户。

    picasaweb-current-setup.exe

    picasaweb-current-setup.exe

    Picasa谷歌相册管理软件 v3.9.0.136.12.zip

    每次打开 Picasa 时,它都会自动查找所有图片(甚至是那些您已经遗忘的图片),并将它们按日期顺序放在可见的相册中,同时以您易于识别的名称命名文件夹。您可以通过拖放操作来排列相册,还可以添加标签来创建新组。...

    picasawebalbumdownloader:一个能够下载整个 PicasaWeb 相册的简单应用程序

    picasa网络相册下载器 什么是 Picasa 网络相册下载器? 其目的是能够从 picasa 下载整个专辑。 它是 google for windows 的 Picasa 2.0 或 mac 新发布的 iPhoto 的替代品。

    GooglePicasa3picasa网络相册V3.9.0Build(141.259)谷歌图像浏览器官方安装版

    很强大的功能,如果你的...每次打开 Picasa 时,它都会自动查找所有图片(甚至是那些您已经遗忘的图片),并将它们按日期顺序放在可见的相册中,同时以您易于识别的名称命名文件夹.您可以通过拖放操 作来排列相册,还* Fac

    picasa网络相册· 照片管理

    是一款步错的这些好片安慰她·奇文共赏企业和统计2

    谷歌相册管理软件Picasa3.9.136.180

    Picasa(谷歌相册管理软件)是一个可在计算机上查看、整理、修改和共享数码照片的软件,它会让所有这些工作变得简单而有趣。Picasa 不会未经您的允许就删除照片或将照片放到网络上。 - 自动将你的相片从你的数码相机...

    Google Picasa相册辅助工具

    本工具是为Google Picasa相册提供网络[IMG]标签的批量转换工具,使用方法:输入对应的相册地址,待加载完成,点击转换按钮,再点击复制按钮到剪贴板即可。

    use pwa to access Google picasaweb

    使用pwa访问Google的网络相册,图片要有分类,于是我就简单的改造了一下pwa,效果如下: 演示: http://www.shineid.net/show.asp

    picasa:一个适用于Node.js的简单Google相册(正式称为Picasa网络相册2.0)客户端

    一个简单的Google相册,正式用于Node.js(&gt; = 4.8.7)的Picasa网络相册客户端(2.0)。 包括Auth助手。安装$ yarn add picasa或者$ npm install --save picasa用法const Picasa = require ( 'picasa' )const picasa ...

    Simple Picasaweb Face-开源

    直接在您自己的站点上显示Picasaweb中的相册和照片。 使用Picasaweb API检索您的picasaweb内容。 图片存储在Google的picasaweb中,不需要空间。 基于PHP。

    谷歌相册管理软件 Picasa 3.6.95.25/3.1.71.48

    Picasa原为独立收费的图像管理、处理软件,其界面美观华丽,功能实用丰富。现已改为免费软件。只需拥有Picasa,你就可以随心所欲地体验你的数码相片了。 此下载包括:支持简体中文界面的多语言版和英文版。 ...

    picasa39-setup.zip

    picasa 图片编辑器 picasa 图片编辑器 picasa 图片编辑器 picasa 图片编辑器 picasa 图片编辑器 picasa 图片编辑器 picasa 图片编辑器 picasa 图片编辑器 picasa 图片编辑器 picasa 图片编辑器 picasa ...

    PicasaWeb Mobile-开源

    PicasaWeb Mobile是一个J2ME应用程序,您可以使用它在移动设备上查看picasaweb照片。 公开和私人(需要登录)相册都可以查看。

    谷歌相册管理软件 Picasa v3.9.0.136.12

    每次打开 Picasa 时,它都会自动查找所有图片(甚至是那些您已经遗忘的图片),并将它们按日期顺序放在可见的相册中,同时以您易于识别的名称命名文件夹。您可以通过拖放操作来排列相册,还可以添加标签来创建新组。...

Global site tag (gtag.js) - Google Analytics