java - 如何使用jsoup从一个需要登陆的网站下载图片

 TXCWB_523 发布于 2022-10-28 19:56

有图片的地址,如:http://i2.pixiv.net/img-original/img/201...
还有登陆后获取的cooikes:Connection cookies(Map cookies);
登陆和获取cooikes以及解析页面是使用的jsoup。
但是需要登陆后才能获取,使用以下代码:

private void downloadImg(String imgURL) throws MalformedURLException, IOException {
        URL url = new URL(imgURL);
        URLConnection uc = url.openConnection();
        InputStream is = uc.getInputStream();
        File file = new File("D:\\pixiv");
        FileOutputStream out = new FileOutputStream(file);
        int i = 0;

        while ((i = is.read()) != -1) {
            out.write(i);
        }
        is.close();
    }

只能下载不需要登陆的页面的图片。
要如何使用jsoup带cooikes来从网站下载图片

4 个回答
  • apache httpClient 模拟登陆

    2022-11-12 01:45 回答
  • 提取img src后,如果用URLConnection 下载图片有没有权限,看下网站中有没有session_id之类的东西,总之就是找出用户登录的标识,在img src中带上些标识

    2022-11-12 01:45 回答
  • 不管你用什么解析HTML,唯一确定你登录的就是http请求中的cookie,所以你可以先发生登录请求,从http res中拿到cookie,之后再将cookie设置到下一次http请求中,就完成了浏览器保持cookie的操作,需要登录的资源数据就可以下载了

    2022-11-12 01:45 回答
  • 问题已解决,使用Firebug抓取浏览器中浏览图片时发送的请求包然后再根据其格式,使用URLConnection构造发送带cookies的请求包即可。就是对于大于1M的图片下载速度太慢了。

    /**
         * 下载图片从URL
         *
         * @param img 图片对象
         * @param imgFile 代写入文件对象
         * @throws MalformedURLException 获取URL异常
         * @throws IOException URLConnection获取异常
         */
        public void downloadImg(Img img, File imgFile) throws MalformedURLException, IOException {
            URL url = new URL(img.getUrl());
            URLConnection uc = url.openConnection();
            uc.setConnectTimeout(Setting._Download_Img_TimeOut); // 设置下载图片超时时间
            uc.setRequestProperty("accept", "image/png,image/*;q=0.8,*/*;q=0.5");
            uc.setRequestProperty("accept-encoding", "gzip, deflate");
            uc.setRequestProperty("accept-language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
            uc.setRequestProperty("connection", "keep-alive");
            uc.setRequestProperty("cookie", PixivLogin.userCookies.toString());//这里是cookie部分
            uc.setRequestProperty("dnt", "1");
            uc.setRequestProperty("host", "i2.pixiv.net");
            uc.setRequestProperty("user-agent", Setting._DownLoadImgClient_UserAgent);
    
            uc.setDoInput(true);
            uc.setDoOutput(true);
            System.out.println("图片获取成功");
            System.out.println("开始写入硬盘");
            InputStream is = uc.getInputStream();
            FileOutputStream out = new FileOutputStream(imgFile);
            //BufferedOutputStream bout = new BufferedOutputStream(out);
            int i = 0;
    
            while ((i = is.read()) != -1) {
                out.write(i);
            }
            is.close();
            System.out.println(img.getName() + "写入完毕 " + imgFile.length());
        }
    2022-11-12 01:45 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有