2019独角兽企业重金招聘Python工程师标准>>>
在网上找过相关的资料,都不是太全~有些缺漏。所有自己根据网上的再结合自己的修改了下。
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集合是所有相册的集合。
2.主要是两个方法一个是得到集合getAlbums 一个是保存相册的 savePhoto
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.
/*** 获取用户相册** @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.下载一个相册。
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