Google Picasa是一个在线的相册系统,他提供了很多API,我们可以对相册进行操作,下面就是一个简单的获得一个用户所有相册,并把相册里面所有的图片显示出来,页面我也没有做什么美化,在初步研究之后,发现它的API的功能还比较强大,以后有时间了再看看其他的功能。源代码和.war文件都太大了,上传两个图片看看。。


[size=medium]

Java代码  
  1. import java.io.BufferedInputStream;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.util.ArrayList;
  9. import java.util.Hashtable;
  10. import java.util.List;
  11. import java.util.Map;
  12. import javax.servlet.ServletException;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import com.google.gdata.client.photos.PicasawebService;
  17. import com.google.gdata.data.Link;
  18. import com.google.gdata.data.photos.AlbumEntry;
  19. import com.google.gdata.data.photos.AlbumFeed;
  20. import com.google.gdata.data.photos.GphotoEntry;
  21. import com.google.gdata.data.photos.PhotoEntry;
  22. import com.google.gdata.data.photos.UserFeed;
  23. import com.google.gdata.util.AuthenticationException;
  24. import com.google.gdata.util.ServiceException;
  25. public class PicasaServlet extends HttpServlet {
  26. /**
  27. * This is a serial version generated by eclipse.
  28. */
  29. private static final long serialVersionUID = -6737329335179440491L;
  30. /**
  31. * This is the api prefix of the goold picasa forum.
  32. */
  33. private static final String API_PREFIX = "http://picasaweb.google.com/data/feed/api/user/";
  34. /**
  35. * This method is to deal with the HTTP GET request.
  36. *
  37. * @param request
  38. *            The HttPServletRequest object.
  39. * @param response
  40. *            The HttpServletResponse object.
  41. * @throws ServletException
  42. *             Throws servlet exception if encounters some server errors.
  43. * @throws IOException
  44. *             Throws IOException if encounter some io write/read errors.
  45. */
  46. @Override
  47. protected void doGet(HttpServletRequest request,
  48. HttpServletResponse response) throws ServletException, IOException {
  49. doPost(request, response);
  50. }
  51. /**
  52. * This method is to deal with the HTTP Post request. Download the image and
  53. * corresponding thumbnails image with emailAddress and password
  54. * authentication, and save it to "/image" folder, this can be displayed
  55. * directly in the client.
  56. *
  57. * @param request
  58. *            The HttPServletRequest object.
  59. * @param response
  60. *            The HttpServletResponse object.
  61. * @throws ServletException
  62. *             Throws servlet exception if encounters some server errors.
  63. * @throws IOException
  64. *             Throws IOException if encounter some io write/read errors.
  65. */
  66. @SuppressWarnings("deprecation")
  67. @Override
  68. protected void doPost(HttpServletRequest request,
  69. HttpServletResponse response) throws ServletException, IOException {
  70. String emailAddress = request.getParameter("emailAddress");
  71. String password = request.getParameter("password");
  72. String realPath = request.getRealPath("/image");
  73. Map<String, List<String>> albumPhotos = generatePhoto(emailAddress,
  74. password, realPath);
  75. request.setAttribute("albumPhotos", albumPhotos);
  76. request.getRequestDispatcher("/result.jsp").forward(request, response);
  77. }
  78. /**
  79. * This method is to get the album, and all the photo's name and thumbnail
  80. * format name, and the photo's description.
  81. *
  82. * @param emailAddress
  83. *            The email address which you used to authenticated.
  84. * @param password
  85. *            The password which you used to authenticated.
  86. * @param realPath
  87. *            The realPath you want to save the images.
  88. * @return Map<String,List<Sring>> which represents the album and all the
  89. *         photos in that album. It always have the following format.
  90. *         albumName: test.png i_test.png This is the image's description.
  91. * @throws IOException
  92. *             Throws IOException if encounter some io write/read errors.
  93. */
  94. public Map<String, List<String>> generatePhoto(String emailAddress,
  95. String password, String realPath) throws IOException {
  96. if (emailAddress != null && password != null) {
  97. try {
  98. PicasawebService picasaService = buildPicasaWebService(
  99. "Picasa", emailAddress, password);
  100. List<AlbumEntry> albums = getAlbums(picasaService, emailAddress);
  101. Map<String, List<String>> albumPhotos = new Hashtable<String, List<String>>();
  102. for (AlbumEntry albumEntry : albums) {
  103. List<PhotoEntry> photoEntrys = getPhotos(picasaService,
  104. albumEntry);
  105. if (photoEntrys.size() > 0) {
  106. List<String> photos = new ArrayList<String>();
  107. for (PhotoEntry photoEntry : photoEntrys) {
  108. String thumbnailsName = writeImage(photoEntry
  109. .getMediaThumbnails().get(0).getUrl(),
  110. realPath, false);
  111. String originalName = writeImage(photoEntry
  112. .getMediaContents().get(0).getUrl(),
  113. realPath, true);
  114. photos.add(thumbnailsName);
  115. photos.add(originalName);
  116. photos.add(photoEntry.getDescription()
  117. .getPlainText());
  118. }
  119. albumPhotos.put(albumEntry.getName(), photos);
  120. }
  121. }
  122. return albumPhotos;
  123. } catch (AuthenticationException e) {
  124. e.printStackTrace();
  125. } catch (ServiceException e) {
  126. e.printStackTrace();
  127. }
  128. }
  129. return new Hashtable<String, List<String>>();
  130. }
  131. /**
  132. * This is method is to build a PicasawebService object using the
  133. * emailAddress and password, and the appName.
  134. *
  135. * @param appName
  136. *            The appname you want to set, in this program we set it
  137. *            "picasa"
  138. * @param emailAddress
  139. *            The email address of you used to authenticated.
  140. * @param password
  141. *            The password you used to authenticated.
  142. * @return The PicasawebService object.
  143. * @throws AuthenticationException
  144. *             If there exist some authentication exception occur.
  145. */
  146. public PicasawebService buildPicasaWebService(String appName,
  147. String emailAddress, String password)
  148. throws AuthenticationException {
  149. PicasawebService picasaService = new PicasawebService(appName);
  150. picasaService.setUserCredentials(emailAddress, password);
  151. return picasaService;
  152. }
  153. /**
  154. * This method is to extract a user's album.
  155. *
  156. * @param picasaService
  157. *            The picasawebService object, we use this object to get the
  158. *            user's feed.
  159. * @param username
  160. *            The user name which we want to get the all the album.
  161. * @return A list of AlbumEntrys.
  162. * @throws IOException
  163. *             There is some write/read exception occured.
  164. * @throws ServiceException
  165. *             This is some service exception occur, for example, the wrong
  166. *             URL
  167. */
  168. @SuppressWarnings("unchecked")
  169. public List<AlbumEntry> getAlbums(PicasawebService picasaService,
  170. String username) throws IOException, ServiceException {
  171. String albumUrl = API_PREFIX + username;
  172. UserFeed userFeed = picasaService.getFeed(new URL(albumUrl),
  173. UserFeed.class);
  174. List<GphotoEntry> entries = userFeed.getEntries();
  175. List<AlbumEntry> albums = new ArrayList<AlbumEntry>();
  176. for (GphotoEntry entry : entries) {
  177. GphotoEntry adapted = entry.getAdaptedEntry();
  178. if (adapted instanceof AlbumEntry) {
  179. albums.add((AlbumEntry) adapted);
  180. }
  181. }
  182. return albums;
  183. }
  184. /**
  185. * This method is to extract all the photos from a particular album.
  186. *
  187. * @param picasaService
  188. *            The picasawebService object, we use this object to get the
  189. *            user's feed.
  190. * @param album
  191. *            The AlbumEntry object which you want to get all the photos
  192. *            from it.
  193. * @return A list of PhotoEntry object.
  194. * @throws IOException
  195. *             There is some write/read exception occured.
  196. * @throws ServiceException
  197. *             This is some service exception occur, for example, the wrong
  198. *             URL
  199. */
  200. @SuppressWarnings("unchecked")
  201. public List<PhotoEntry> getPhotos(PicasawebService picasaService,
  202. AlbumEntry album) throws IOException, ServiceException {
  203. String feedHref = getLinkByRel(album.getLinks(), Link.Rel.FEED);
  204. AlbumFeed albumFeed = picasaService.getFeed(new URL(feedHref),
  205. AlbumFeed.class);
  206. List<GphotoEntry> entries = albumFeed.getEntries();
  207. List<PhotoEntry> photos = new ArrayList<PhotoEntry>();
  208. for (GphotoEntry entry : entries) {
  209. GphotoEntry adapted = entry.getAdaptedEntry();
  210. if (adapted instanceof PhotoEntry) {
  211. photos.add((PhotoEntry) adapted);
  212. }
  213. }
  214. return photos;
  215. }
  216. /**
  217. * This method is a helper method, is to get the href from with a link's
  218. * name from the corresponding links collection.
  219. *
  220. * @param links
  221. *            The Link collection you want to elect from.
  222. * @param relValue
  223. *            The corresponding value.
  224. * @return The real link's href value.
  225. */
  226. public String getLinkByRel(List<Link> links, String relValue) {
  227. for (Link link : links) {
  228. if (relValue.equals(link.getRel())) {
  229. return link.getHref();
  230. }
  231. }
  232. throw new IllegalArgumentException("Missing " + relValue + " link.");
  233. }
  234. /**
  235. * This method is to write the urlAddress's image to the server's "/image"
  236. * folder, and if it is the thumbnail, we just directly add a "i_" prefix to
  237. * distinguish,
  238. *
  239. * @param urlAddress
  240. *            The image URL address, it is a String object.
  241. * @param realPath
  242. *            The real path of "/image" folder, in that path you want to
  243. *            save this image.
  244. * @param isThumbnail
  245. *            Whether the image is thumbnail.
  246. * @return The filename of the image, if it is thumbnail manner.
  247. */
  248. public String writeImage(String urlAddress, String realPath,
  249. boolean isThumbnail) {
  250. try {
  251. URL url = new URL(urlAddress);
  252. BufferedInputStream bis = new BufferedInputStream(url.openStream());
  253. String fileName = getFileName(urlAddress, isThumbnail);
  254. byte[] bytes = new byte[1024];
  255. OutputStream bos = new FileOutputStream(new File(realPath + "\\"
  256. + fileName));
  257. int len;
  258. while ((len = bis.read(bytes)) > 0) {
  259. bos.write(bytes, 0, len);
  260. }
  261. bis.close();
  262. bos.flush();
  263. bos.close();
  264. return fileName;
  265. } catch (MalformedURLException e) {
  266. e.printStackTrace();
  267. } catch (IOException e) {
  268. e.printStackTrace();
  269. }
  270. return "";
  271. }
  272. /**
  273. * This method is to get the file name from the URL address, and if it is
  274. * thumbnail, add a "i_" prefix to distinguish.
  275. *
  276. * @param urlAddress
  277. *            The URL address that contains the real image address.
  278. * @param isThumbnail
  279. *            Whether this image is thumbnail
  280. * @return The formated filename of this image represented by the URL
  281. *         address.
  282. */
  283. public String getFileName(String urlAddress, boolean isThumbnail) {
  284. int lastURLSeparater = urlAddress.lastIndexOf("/");
  285. if (isThumbnail) {
  286. return urlAddress.substring(lastURLSeparater + 1);
  287. } else {
  288. return "i_" + urlAddress.substring(lastURLSeparater + 1);
  289. }
  290. }
  291. }

[/size]
[size=medium]

Html代码  
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  4. "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Input the emailAddress and password</title>
  9. </head>
  10. <body>
  11. <form action="servlet/picasaServlet" method="post">
  12. <label>Input emailAddress:</label>
  13. <input type="text" name="emailAddress" />
  14. <label>Input password:</label>
  15. <input type="password"" name="password" />
  16. <input type="submit" value="submit">
  17. </form>
  18. </body>
  19. </html>

[/size]
[size=medium]

Html代码  
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ page import="java.util.*" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  5. "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <title>Input the emailAddress and password</title>
  10. </head>
  11. <body>
  12. <%
  13. Map<String,List<String>> albumPhotos =
  14. (Map<String,List<String>>)request.getAttribute("albumPhotos");
  15. for(Iterator<String> it = albumPhotos.keySet().iterator(); it.hasNext();){
  16. String albumName = it.next();
  17. List<String> photos = albumPhotos.get(albumName);
  18. %>
  19. albumName:<%=albumName%><br>
  20. <%for(int i = 0; i < photos.size(); i+=3){%>
  21. Thumbnails: <img alt="" src="<%=request.getContextPath()%>/image/<%=photos.get(i)%>"><br>
  22. Original: <img alt="" src="<%=request.getContextPath()%>/image/<%=photos.get(i+1)%>"><br>
  23. Description: <%=photos.get(i+2)%>
  24. <%
  25. }
  26. }
  27. %>
  28. </body>
  29. </html>

[/size]

Google Picasa API初体验相关推荐

  1. ap接口 php_小白php API初体验 php api文档 php api接口开发 php web ap

    这里的php 写API其实就是指提供一个WebServiceWebSite : 1.以html格式响应返回 2.由用户通过浏览器来接入 WebService : 1.以json/Xml格式返回 2.由 ...

  2. Kong Api 初体验、Kong安装教程

    见:https://blog.csdn.net/forezp/article/details/79383631 Kong是一个可扩展的开源API层(也称为API网关或API中间件). Kong运行在任 ...

  3. Kong Api 初体验

    个人博客纯净版:https://www.fangzhipeng.com/%E6%9E%B6%E6%9E%84/2017/09/17/kong-api-gateway.html Kong是一个可扩展的开 ...

  4. Vue3 组合式API初体验

    目录 一.背景 二.什么是组合式API(Composition API ) 组合式API全景 为什么要引入组合式API `mixins` 的方式 域插槽的方式 组合式API的方式 结论 组合式API存 ...

  5. 2BizBox免费ERP API初体验

    简介 什么是2BizBox API 2BizBox是免费的ERP软件,也是一个开放的ERP平台.2BizBox面向开发者提供了完整的API二次开发接口,用于对2BizBox进行集成和二次开发.2Biz ...

  6. 在 npm 发布中文 API 初体验——中国历代纪元

    发布细节参考中文代码演示--创建 Node.js 模块过程,没什么意外. 数据来源是新华字典第 11 版附录"我国历代纪元简表",以后慢慢细化,先做个草稿. 现在只有一个接口: v ...

  7. Vue3通透教程【四】Vue3组合API初体验

    文章目录

  8. Google APIs .net 客户端库初体验

    今天看到google api .net库的发布这条消息, 初步看了一下相关的内容,这个库对.net程序员和google服务的交互很是方便. 谷歌已经以开源形式发布了.NET APIs Client L ...

  9. vue3.0 Composition API 上手初体验 使用 vue-router 构建多页面应用

    vue3.0 Composition API 上手初体验 使用 vue-router 构建多页面应用 前两讲,我们已经顺利的使用 vue3.0 将项目跑起来了.但是实在是过于简陋,目前我们几乎不可能开 ...

  10. MapReduce编程初体验

    需求:在给定的文本文件中统计输出每一个单词出现的总次数 第一步: 准备一个aaa.txt文本文档 第二步: 在文本文档中随便写入一些测试数据,这里我写入的是 hello,world,hadoop he ...

最新文章

  1. 聊聊Cassandra的FailureDetector
  2. DotNet的JSON序列化与反序列化
  3. OSPF中 hello报文的 内容
  4. Flex AdvancedDataGrid 数据展示异常
  5. 物联网处理器定义混沌不明,市场尚未成熟
  6. uni-app手机横屏后界面错乱解决办法
  7. 《Linux编程》课堂测验 ·002【Shell编程】
  8. 客户端主机自查DNS故障及应急解决办法
  9. Socket(网络编程)面试题
  10. 通过R访问世界银行数据(World Bank Data)分析经济
  11. [源码解读]一文彻底搞懂Events模块
  12. 47 WebGL雾化(大气效果)
  13. TypeError: Converting circular structure to JSON
  14. 常识-java发送邮件函数+开启qq邮箱授权码
  15. 手机投屏电视显示服务器连接失败,投屏失败怎么办?两种投屏到电视的方法教学...
  16. 最新特效移动文字代码大全
  17. 全国中学生计算机大赛+试题,全国青少年信息学奥林匹克竞赛(NOI2018)正式开幕(附day1试题)...
  18. 2021-2022-2-第5次单元练习后记
  19. 强制旋转iPhone界面
  20. 干货!ERP软件选型前一定要考虑的四大问题

热门文章

  1. python高斯滤波和降噪_高斯滤波详解 附python和matlab高斯滤波代码
  2. ppt插入计算机时间,WPS之PPT插入自动更新的时间设置
  3. 我想向你们推荐一门最好的python课程——CS61A学习笔记(一)
  4. 应用matlab软件编写 t检验,应用matlab软件进行方差分析 应用方差分析的前提条件...
  5. 本特利3300XL 25mm前置器 330780-50-CN
  6. 51单片机的篮球计分器设计
  7. RPA - 前置机虚拟化U盾识别方案
  8. 乾颐堂现任明教教主(2014年课程)TCPIP协议详解卷一 第九节课笔记
  9. 松下新一代电力线通信(PLC)技术经IEEE P1901.3工作小组批准成为基准规范
  10. 笔记本电池续航测试软件,电池续航能力测试及整机试用总结