1. package com.springsecurity.demo1.utils;
    2. import com.alibaba.fastjson.JSON;
    3. import okhttp3.*;
    4. import org.springframework.scheduling.annotation.Scheduled;
    5. import org.springframework.stereotype.Component;
    6. import org.springframework.util.CollectionUtils;
    7. import java.io.IOException;
    8. import java.util.List;
    9. import java.util.Map;
    10. @Component
    11. public class WeiBoImgDow {
    12. private static String url = "https://wx1.sinaimg.cn/mw2000/";
    13. private static String since_id = "0";
    14. private static String uid = "3564019983";
    15. @Scheduled(cron = "*/100 * * * * ?")//每个5秒执行一次任务(方法)
    16. public void timeScheduled(){
    17. OkHttpClient client = new OkHttpClient();
    18. Request request = new Request.Builder()
    19. .url("https://weibo.com/ajax/profile/getImageWall?uid="+uid+"&has_album=true&sinceid=" + since_id)
    20. .get()
    21. .addHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36")
    22. .addHeader("cookie", "SINAGLOBAL=5965561176493.22.1649506152936; UOR=,,login.sina.com.cn; SCF=AiWwgnCYo9d4EReJyOmefZdZutv_V6XZmbNTPCF9wt-c4FAF1LbEPnsIu9XyMCO5VMvB8PT6Y5mVOVWn9I-feN4.; _s_tentry=weibo.com; Apache=8751628401274.005.1654596667566; ULV=1654596667569:5:1:1:8751628401274.005.1654596667566:1653962739874; XSRF-TOKEN=rUFmn_wuBQoZPL_pM1K7Va1S; SUB=_2AkMVw6PbdcPxrARWmP0SzWzjaIxH-jymFsotAn7uJhMyAxgP7mcUqSVutBF-XGzfXuV98PjN_Hsy0n7q1luJ2D3y; SUBP=0033WrSXqPxfM72wWs9jqgMF55529P9D9WhcHcCnEJp.vxweY0mLM20u; WBPSESS=Dt2hbAUaXfkVprjyrAZT_PX83v-WoAUYYauXtITmgP77DvugoT9KL_F1k8lDUHCvGfgVebA5Qs1_Ut3l52hPFHn19s3g5eoFZ3-Qy1lPWYtZVHCJ4a1rO2dgvmWQ6W0H5Qy1DcTo1LJR3Vfl83BpqQ==")
    23. .addHeader("Content-Type", "application/json; charset=utf-8")
    24. .build();
    25. Call call = client.newCall(request);
    26. call.enqueue(new Callback() {
    27. //请求失败执行的方法
    28. @Override
    29. public void onFailure(Call call, IOException e) {
    30. System.out.println(e.getMessage());
    31. }
    32. //请求成功执行的方法
    33. @Override
    34. public void onResponse(Call call, Response response) throws IOException {
    35. String data = response.body().string();
    36. final Map map = JSON.parseObject(data, Map.class);
    37. final Object data1 = map.get("data");
    38. final Map map2 = JSON.parseObject(JSON.toJSONString(data1), Map.class);
    39. since_id = String.valueOf(map2.get("since_id"));
    40. Object list = map2.get("list");
    41. if (list == null){
    42. return;
    43. }
    44. List list1 = JSON.parseObject(JSON.toJSONString(list), List.class);
    45. if (CollectionUtils.isEmpty(list1)){
    46. return;
    47. }
    48. for (Object o : list1) {
    49. Map map1 = JSON.parseObject(JSON.toJSONString(o), Map.class);
    50. Object pid = map1.get("pid");
    51. String durl = url + pid;
    52. try {
    53. Download.download(durl,pid,uid);
    54. } catch (Exception e) {
    55. e.printStackTrace();
    56. }
    57. }
    58. }
    59. });
    60. }
    61. }
    package com.springsecurity.demo1.utils;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;
    
    public class Download implements Runnable{
        private String urlString;
        private Object name;
        private String uid;
        public Download(String urlString,Object name,String uid){
            this.urlString =urlString;
            this.name =name;
            this.uid =uid;
        }
    
        @Override
        public void run() {
            try {
                download( urlString,  name, uid);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //java 通过url下载图片保存到本地
        public void download(String urlString, Object name,String uid) throws Exception {
            // 构造URL
            URL url = new URL(urlString);
            // 打开连接
            URLConnection con = url.openConnection();
            // 输入流
            InputStream is = con.getInputStream();
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            String address = "/Users/tofflon/Downloads/images/" + uid;
            File file2 = new File(address); //以某路径实例化一个File对象
            if (!file2.exists()){ //如果不存在
                file2.mkdirs(); //创建目录
            }
            // 输出的文件流  //下载路径及下载图片名称
            String filename = address +"/"+name+".jpg";
            File file = new File(filename);
            FileOutputStream os = new FileOutputStream(file, true);
            // 开始读取
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
            System.out.println(name);
            // 完毕,关闭所有链接
            os.close();
            is.close();
        }
    
    
    }