Google Picasa API初体验
Google Picasa是一个在线的相册系统,他提供了很多API,我们可以对相册进行操作,下面就是一个简单的获得一个用户所有相册,并把相册里面所有的图片显示出来,页面我也没有做什么美化,在初步研究之后,发现它的API的功能还比较强大,以后有时间了再看看其他的功能。源代码和.war文件都太大了,上传两个图片看看。。
[size=medium]
- 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.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- 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;
- public class PicasaServlet extends HttpServlet {
- /**
- * This is a serial version generated by eclipse.
- */
- private static final long serialVersionUID = -6737329335179440491L;
- /**
- * This is the api prefix of the goold picasa forum.
- */
- private static final String API_PREFIX = "http://picasaweb.google.com/data/feed/api/user/";
- /**
- * This method is to deal with the HTTP GET request.
- *
- * @param request
- * The HttPServletRequest object.
- * @param response
- * The HttpServletResponse object.
- * @throws ServletException
- * Throws servlet exception if encounters some server errors.
- * @throws IOException
- * Throws IOException if encounter some io write/read errors.
- */
- @Override
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- doPost(request, response);
- }
- /**
- * This method is to deal with the HTTP Post request. Download the image and
- * corresponding thumbnails image with emailAddress and password
- * authentication, and save it to "/image" folder, this can be displayed
- * directly in the client.
- *
- * @param request
- * The HttPServletRequest object.
- * @param response
- * The HttpServletResponse object.
- * @throws ServletException
- * Throws servlet exception if encounters some server errors.
- * @throws IOException
- * Throws IOException if encounter some io write/read errors.
- */
- @SuppressWarnings("deprecation")
- @Override
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- String emailAddress = request.getParameter("emailAddress");
- String password = request.getParameter("password");
- String realPath = request.getRealPath("/image");
- Map<String, List<String>> albumPhotos = generatePhoto(emailAddress,
- password, realPath);
- request.setAttribute("albumPhotos", albumPhotos);
- request.getRequestDispatcher("/result.jsp").forward(request, response);
- }
- /**
- * This method is to get the album, and all the photo's name and thumbnail
- * format name, and the photo's description.
- *
- * @param emailAddress
- * The email address which you used to authenticated.
- * @param password
- * The password which you used to authenticated.
- * @param realPath
- * The realPath you want to save the images.
- * @return Map<String,List<Sring>> which represents the album and all the
- * photos in that album. It always have the following format.
- * albumName: test.png i_test.png This is the image's description.
- * @throws IOException
- * Throws IOException if encounter some io write/read errors.
- */
- public Map<String, List<String>> generatePhoto(String emailAddress,
- String password, String realPath) throws IOException {
- if (emailAddress != null && password != null) {
- try {
- PicasawebService picasaService = buildPicasaWebService(
- "Picasa", emailAddress, password);
- List<AlbumEntry> albums = getAlbums(picasaService, emailAddress);
- Map<String, List<String>> albumPhotos = new Hashtable<String, List<String>>();
- for (AlbumEntry albumEntry : albums) {
- List<PhotoEntry> photoEntrys = getPhotos(picasaService,
- albumEntry);
- if (photoEntrys.size() > 0) {
- List<String> photos = new ArrayList<String>();
- for (PhotoEntry photoEntry : photoEntrys) {
- String thumbnailsName = writeImage(photoEntry
- .getMediaThumbnails().get(0).getUrl(),
- realPath, false);
- String originalName = writeImage(photoEntry
- .getMediaContents().get(0).getUrl(),
- realPath, true);
- photos.add(thumbnailsName);
- photos.add(originalName);
- photos.add(photoEntry.getDescription()
- .getPlainText());
- }
- albumPhotos.put(albumEntry.getName(), photos);
- }
- }
- return albumPhotos;
- } catch (AuthenticationException e) {
- e.printStackTrace();
- } catch (ServiceException e) {
- e.printStackTrace();
- }
- }
- return new Hashtable<String, List<String>>();
- }
- /**
- * This is method is to build a PicasawebService object using the
- * emailAddress and password, and the appName.
- *
- * @param appName
- * The appname you want to set, in this program we set it
- * "picasa"
- * @param emailAddress
- * The email address of you used to authenticated.
- * @param password
- * The password you used to authenticated.
- * @return The PicasawebService object.
- * @throws AuthenticationException
- * If there exist some authentication exception occur.
- */
- public PicasawebService buildPicasaWebService(String appName,
- String emailAddress, String password)
- throws AuthenticationException {
- PicasawebService picasaService = new PicasawebService(appName);
- picasaService.setUserCredentials(emailAddress, password);
- return picasaService;
- }
- /**
- * This method is to extract a user's album.
- *
- * @param picasaService
- * The picasawebService object, we use this object to get the
- * user's feed.
- * @param username
- * The user name which we want to get the all the album.
- * @return A list of AlbumEntrys.
- * @throws IOException
- * There is some write/read exception occured.
- * @throws ServiceException
- * This is some service exception occur, for example, the wrong
- * URL
- */
- @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;
- }
- /**
- * This method is to extract all the photos from a particular album.
- *
- * @param picasaService
- * The picasawebService object, we use this object to get the
- * user's feed.
- * @param album
- * The AlbumEntry object which you want to get all the photos
- * from it.
- * @return A list of PhotoEntry object.
- * @throws IOException
- * There is some write/read exception occured.
- * @throws ServiceException
- * This is some service exception occur, for example, the wrong
- * URL
- */
- @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;
- }
- /**
- * This method is a helper method, is to get the href from with a link's
- * name from the corresponding links collection.
- *
- * @param links
- * The Link collection you want to elect from.
- * @param relValue
- * The corresponding value.
- * @return The real link's href value.
- */
- public String getLinkByRel(List<Link> links, String relValue) {
- for (Link link : links) {
- if (relValue.equals(link.getRel())) {
- return link.getHref();
- }
- }
- throw new IllegalArgumentException("Missing " + relValue + " link.");
- }
- /**
- * This method is to write the urlAddress's image to the server's "/image"
- * folder, and if it is the thumbnail, we just directly add a "i_" prefix to
- * distinguish,
- *
- * @param urlAddress
- * The image URL address, it is a String object.
- * @param realPath
- * The real path of "/image" folder, in that path you want to
- * save this image.
- * @param isThumbnail
- * Whether the image is thumbnail.
- * @return The filename of the image, if it is thumbnail manner.
- */
- public String writeImage(String urlAddress, String realPath,
- boolean isThumbnail) {
- try {
- URL url = new URL(urlAddress);
- BufferedInputStream bis = new BufferedInputStream(url.openStream());
- String fileName = getFileName(urlAddress, isThumbnail);
- byte[] bytes = new byte[1024];
- OutputStream bos = new FileOutputStream(new File(realPath + "\\"
- + fileName));
- int len;
- while ((len = bis.read(bytes)) > 0) {
- bos.write(bytes, 0, len);
- }
- bis.close();
- bos.flush();
- bos.close();
- return fileName;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return "";
- }
- /**
- * This method is to get the file name from the URL address, and if it is
- * thumbnail, add a "i_" prefix to distinguish.
- *
- * @param urlAddress
- * The URL address that contains the real image address.
- * @param isThumbnail
- * Whether this image is thumbnail
- * @return The formated filename of this image represented by the URL
- * address.
- */
- public String getFileName(String urlAddress, boolean isThumbnail) {
- int lastURLSeparater = urlAddress.lastIndexOf("/");
- if (isThumbnail) {
- return urlAddress.substring(lastURLSeparater + 1);
- } else {
- return "i_" + urlAddress.substring(lastURLSeparater + 1);
- }
- }
- }
[/size]
[size=medium]
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Input the emailAddress and password</title>
- </head>
- <body>
- <form action="servlet/picasaServlet" method="post">
- <label>Input emailAddress:</label>
- <input type="text" name="emailAddress" />
- <label>Input password:</label>
- <input type="password"" name="password" />
- <input type="submit" value="submit">
- </form>
- </body>
- </html>
[/size]
[size=medium]
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ page import="java.util.*" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Input the emailAddress and password</title>
- </head>
- <body>
- <%
- Map<String,List<String>> albumPhotos =
- (Map<String,List<String>>)request.getAttribute("albumPhotos");
- for(Iterator<String> it = albumPhotos.keySet().iterator(); it.hasNext();){
- String albumName = it.next();
- List<String> photos = albumPhotos.get(albumName);
- %>
- albumName:<%=albumName%><br>
- <%for(int i = 0; i < photos.size(); i+=3){%>
- Thumbnails: <img alt="" src="<%=request.getContextPath()%>/image/<%=photos.get(i)%>"><br>
- Original: <img alt="" src="<%=request.getContextPath()%>/image/<%=photos.get(i+1)%>"><br>
- Description: <%=photos.get(i+2)%>
- <%
- }
- }
- %>
- </body>
- </html>
[/size]
Google Picasa API初体验相关推荐
- ap接口 php_小白php API初体验 php api文档 php api接口开发 php web ap
这里的php 写API其实就是指提供一个WebServiceWebSite : 1.以html格式响应返回 2.由用户通过浏览器来接入 WebService : 1.以json/Xml格式返回 2.由 ...
- Kong Api 初体验、Kong安装教程
见:https://blog.csdn.net/forezp/article/details/79383631 Kong是一个可扩展的开源API层(也称为API网关或API中间件). Kong运行在任 ...
- Kong Api 初体验
个人博客纯净版:https://www.fangzhipeng.com/%E6%9E%B6%E6%9E%84/2017/09/17/kong-api-gateway.html Kong是一个可扩展的开 ...
- Vue3 组合式API初体验
目录 一.背景 二.什么是组合式API(Composition API ) 组合式API全景 为什么要引入组合式API `mixins` 的方式 域插槽的方式 组合式API的方式 结论 组合式API存 ...
- 2BizBox免费ERP API初体验
简介 什么是2BizBox API 2BizBox是免费的ERP软件,也是一个开放的ERP平台.2BizBox面向开发者提供了完整的API二次开发接口,用于对2BizBox进行集成和二次开发.2Biz ...
- 在 npm 发布中文 API 初体验——中国历代纪元
发布细节参考中文代码演示--创建 Node.js 模块过程,没什么意外. 数据来源是新华字典第 11 版附录"我国历代纪元简表",以后慢慢细化,先做个草稿. 现在只有一个接口: v ...
- Vue3通透教程【四】Vue3组合API初体验
文章目录
- Google APIs .net 客户端库初体验
今天看到google api .net库的发布这条消息, 初步看了一下相关的内容,这个库对.net程序员和google服务的交互很是方便. 谷歌已经以开源形式发布了.NET APIs Client L ...
- vue3.0 Composition API 上手初体验 使用 vue-router 构建多页面应用
vue3.0 Composition API 上手初体验 使用 vue-router 构建多页面应用 前两讲,我们已经顺利的使用 vue3.0 将项目跑起来了.但是实在是过于简陋,目前我们几乎不可能开 ...
- MapReduce编程初体验
需求:在给定的文本文件中统计输出每一个单词出现的总次数 第一步: 准备一个aaa.txt文本文档 第二步: 在文本文档中随便写入一些测试数据,这里我写入的是 hello,world,hadoop he ...
最新文章
- 聊聊Cassandra的FailureDetector
- DotNet的JSON序列化与反序列化
- OSPF中 hello报文的 内容
- Flex AdvancedDataGrid 数据展示异常
- 物联网处理器定义混沌不明,市场尚未成熟
- uni-app手机横屏后界面错乱解决办法
- 《Linux编程》课堂测验 ·002【Shell编程】
- 客户端主机自查DNS故障及应急解决办法
- Socket(网络编程)面试题
- 通过R访问世界银行数据(World Bank Data)分析经济
- [源码解读]一文彻底搞懂Events模块
- 47 WebGL雾化(大气效果)
- TypeError: Converting circular structure to JSON
- 常识-java发送邮件函数+开启qq邮箱授权码
- 手机投屏电视显示服务器连接失败,投屏失败怎么办?两种投屏到电视的方法教学...
- 最新特效移动文字代码大全
- 全国中学生计算机大赛+试题,全国青少年信息学奥林匹克竞赛(NOI2018)正式开幕(附day1试题)...
- 2021-2022-2-第5次单元练习后记
- 强制旋转iPhone界面
- 干货!ERP软件选型前一定要考虑的四大问题
热门文章
- python高斯滤波和降噪_高斯滤波详解 附python和matlab高斯滤波代码
- ppt插入计算机时间,WPS之PPT插入自动更新的时间设置
- 我想向你们推荐一门最好的python课程——CS61A学习笔记(一)
- 应用matlab软件编写 t检验,应用matlab软件进行方差分析 应用方差分析的前提条件...
- 本特利3300XL 25mm前置器 330780-50-CN
- 51单片机的篮球计分器设计
- RPA - 前置机虚拟化U盾识别方案
- 乾颐堂现任明教教主(2014年课程)TCPIP协议详解卷一 第九节课笔记
- 松下新一代电力线通信(PLC)技术经IEEE P1901.3工作小组批准成为基准规范
- 笔记本电池续航测试软件,电池续航能力测试及整机试用总结