package com.springsecurity.demo1.utils;import com.alibaba.fastjson.JSON;import okhttp3.*;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import org.springframework.util.CollectionUtils;import java.io.IOException;import java.util.List;import java.util.Map;@Componentpublic class WeiBoImgDow { private static String url = "https://wx1.sinaimg.cn/mw2000/"; private static String since_id = "0"; private static String uid = "3564019983"; @Scheduled(cron = "*/100 * * * * ?")//每个5秒执行一次任务(方法) public void timeScheduled(){ OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://weibo.com/ajax/profile/getImageWall?uid="+uid+"&has_album=true&sinceid=" + since_id) .get() .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") .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==") .addHeader("Content-Type", "application/json; charset=utf-8") .build(); Call call = client.newCall(request); call.enqueue(new Callback() { //请求失败执行的方法 @Override public void onFailure(Call call, IOException e) { System.out.println(e.getMessage()); } //请求成功执行的方法 @Override public void onResponse(Call call, Response response) throws IOException { String data = response.body().string(); final Map map = JSON.parseObject(data, Map.class); final Object data1 = map.get("data"); final Map map2 = JSON.parseObject(JSON.toJSONString(data1), Map.class); since_id = String.valueOf(map2.get("since_id")); Object list = map2.get("list"); if (list == null){ return; } List list1 = JSON.parseObject(JSON.toJSONString(list), List.class); if (CollectionUtils.isEmpty(list1)){ return; } for (Object o : list1) { Map map1 = JSON.parseObject(JSON.toJSONString(o), Map.class); Object pid = map1.get("pid"); String durl = url + pid; try { Download.download(durl,pid,uid); } catch (Exception e) { e.printStackTrace(); } } } }); }}
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();
}
}