bing壁纸获取(Java)

获取bing壁纸

接口及实现

简单的demo,将所有的方法都复制到一个Controller里面了。

/img/url (通过流的方式,可以直接读取到图片)

/img/info (返回图片的信息以json的形式,采用异步非阻塞的方式执行代码)

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;


/**
 * @author zhoujx
 */
@RestController
@RequestMapping("/img")
public class ImgController {

    @RequestMapping("/url")
    public void getImgUrl(HttpServletResponse response) throws IOException{
        JSONObject object = getUrl();
        String url = "https://cn.bing.com"   object.getJSONArray("images").getJSONObject(0).getString("url");
        Objects.requireNonNull(doGetEntity(url)).writeTo(response.getOutputStream());
        response.setContentType("image/jpg");
    }

    @GetMapping("/info")
    public Mono<JSONObject> getImgInfo(){
        return Mono.create(res -> {
            JSONObject object = getUrl();
            res.success(object);
        });
    }

    private JSONObject getUrl(){
        String json = doGet("https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN");
        return JSON.parseObject(json);
    }

    public static String doGet(String url) {
        return getString(url);
    }

    private static String getString(String url) {
        String result = "";
        try {
            HttpGet httpGet = new HttpGet(url);
            HttpClient client = HttpClients.createDefault();
            HttpResponse response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static HttpEntity doGetEntity(String url) {
        return getHttpEntity(url);
    }

    private static HttpEntity getHttpEntity(String url) {
        try {
            HttpGet httpGet = new HttpGet(url);
            HttpClient client = HttpClients.createDefault();
            HttpResponse response = client.execute(httpGet);
            response.getEntity().getContent();
            return response.getEntity();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

接口说明

bing壁纸接口

https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN

参数名称
值含义
format (非必需)返回数据格式,不存在返回xml格式js (一般使用这个,返回json格式)xml(返回xml格式)
idx (非必需)
请求图片截止天数0 今天-1 截止中明天 (预准备的)1 截止至昨天,类推(目前最多获取到7天前的图片)
n (必需)
1-8 返回请求数量,目前最多一次获取8张
mkt (非必需)
地区:zh-CN等

返回的信息

Snipaste_2019-11-12_11-25-46

blog.koreyoshi.work

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值