在网上找过相关的资料,都不是太全~有些缺漏。全部本身根据网上的再结合本身的修改了下。java
1.经过HttpAnalyzerStdV5 分析QQ空间相册的真实地址。一下就是空间相册的地址:服务器
以前在网上看见只有一个地址,可是经过个人分析,貌似会有很大的问题。好比:某我的的空间相册有些是有设置密码的。也有不设置密码的,那么就没法下载。由于这类的相册是经过另一个URL解析的。网络
private static final String albumbase1 = "http://alist.photo.qq.com/fcgi-bin/fcg_list_album?uin=";//若是没有设置密保的相册是经过这个地址访问的 private static final String albumbase2 = "http://xalist.photo.qq.com/fcgi-bin/fcg_list_album?uin=";//设置密保的相册是经过这个地址访问的 //private static final String photobase = "http://alist.photo.qq.com/fcgi-bin/fcg_list_photo?uin="; private static final String photobase1 = "http://plist.photo.qq.com/fcgi-bin/fcg_list_photo?uin="; private static final String photobase2 = "http://xaplist.photo.qq.com/fcgi-bin/fcg_list_photo?uin=";
2.程序的main方法:异步
1.albums集合是全部相册的集合。ui
2.主要是两个方法一个是获得集合getAlbums 一个是保存相册的 savePhotothis
public static void main(String[] args) { PhotoDownLoad pdl = new PhotoDownLoad(); String qq = "1315404564"; albums = pdl.getAlbums(qq, albumbase1);//先传一个地址进去要是找到再也不分析另一个地址 if (albums == null || albums.size() == 0) { albums = pdl.getAlbums(qq, albumbase2); } if (albums == null || albums.size() == 0) { System.out.println("没有获取到相册"); } int len = albums.size(); System.out.println("相册信息获取成功,用户共有" + len + "个相册."); for (int i = 0; i < len; i++) { // 考虑到相册数量不会不少,相册采用顺序下载,不使用异步下载 System.out.println("开始下载第" + (i + 1) + "个相册..."); pdl.savePhoto(i, qq); pdl.curIndex = 0; System.out.println("第" + (i + 1) + "个相册下载完成."); } }
3.getAlbums 方法
1.url
/** * 获取用户相册 * * @param qq * @return */ public List<Album> getAlbums(String qq, String url) { List<Album> result = new ArrayList<Album>(); HttpClient client = new HttpClient(); String getUri = url + qq + "&outstyle=2"; // outstyle!=2服务器将以xml的形式返回结果, HttpMethod method = new GetMethod(getUri); method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset); int status = 0; try { status = client.executeMethod(method); if (status != HttpStatus.SC_OK) { System.err.println("发生网络错误!"); return null; } } catch (HttpException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } InputStream is = null; BufferedReader br = null; InputStreamReader isr = null; List<String> ids = new ArrayList<String>(); List<String> names = new ArrayList<String>(); List<Integer> totals = new ArrayList<Integer>(); try { is = method.getResponseBodyAsStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); String temp = null; while ((temp = br.readLine()) != null) { if (temp.contains("\"id\" :")) { String id = temp.substring(temp.indexOf("\"id\" :") + 8, temp.length() - 2); ids.add(id); } if (temp.contains("\"name\" :")) { String name = temp.substring( temp.indexOf("\"name\" :") + 10, temp.length() - 3); names.add(name); } if (temp.contains("\"total\" :")) { String total = temp .substring(temp.indexOf("\"total\" :") + 10, temp.length() - 1); totals.add(Integer.parseInt(total)); } if (temp.contains("\"left\" :")) { break; } } } catch (IOException e) { e.printStackTrace(); } finally { method.releaseConnection(); try { br.close(); isr.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } for (int i = 0; i < ids.size(); i++) { Album album = new Album(ids.get(i), names.get(i), totals.get(i)); result.add(album); } return result; }
4.下载一个相册。.net
1.获得一个相册的整个图片。线程
/** * 下载一个相册的图片 * * @param index 相册序号 */ public void savePhoto(final int index, final String qq) { Album album = albums.get(index); if(album.getName().indexOf("微博")>=0){ System.out.println("微博相册不下载"); return; } List<Photo> photosTemp = this.getPhotoByAlbum(album, qq, photobase1); if (photosTemp == null || photosTemp.size() == 0) { photosTemp = this.getPhotoByAlbum(album, qq, photobase2); } if (photosTemp == null || photosTemp.size() == 0) { System.out.println("相册信息为空"); return; } else { final List<Photo> photos = photosTemp; final int maxThreadCnt = 10; // 每一个相册最多开启10个线程进行下载 final int total = album.getCnt(); int realThreadCnt = total >= maxThreadCnt ? maxThreadCnt : total; // 实际下载一个相册的线程数 /** * 线程驱动下载任务 * * @author wensefu.jerry.Ling<br/> * wrote on 2011-1-29 */ class DownLoadTask implements Runnable { int id; // 线程标识 int pindex;// 下载的图片指针 public DownLoadTask(int id, int pindex) { this.id = id; this.pindex = pindex; } public void run() { while (curIndex <= total - 1) { int temp = pindex; pindex = curIndex; curIndex++; Photo photo = photos.get(temp); System.out.println("线程" + (index + 1) + "_" + id + "开始下载第" + (index + 1) + "个相册第" + (pindex + 1) + "张图片..."); saveImgFromUrl(photo, qq); System.out.println("线程" + (index + 1) + "_" + id + "完成第" + (index + 1) + "个相册第" + (pindex + 1) + "张图片下载"); } } } ExecutorService exec = Executors.newCachedThreadPool(); /* * 初始化各线程状态 此处给每一个线程分配一个下载起始点 */ for (int i = 0; i < realThreadCnt; i++) { DownLoadTask task = new DownLoadTask(i + 1, i); exec.execute(task); } exec.shutdown(); } }
源代码下载:http://www.oschina.net/code/snippet_557580_12818