通过抓包获取QQ空间相册的真实地址,实现空间相册下载。

在网上找过相关的资料,都不是太全~有些缺漏。所有自己根据网上的再结合自己的修改了下。

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

转载于:https://my.oschina.net/u/557580/blog/72823

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值