bing壁纸获取(Java)

2019-11-12  本文已影响0人  Hello小五

获取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

上一篇下一篇

猜你喜欢

热点阅读