必应(http://cn.bing.com/)每日图片下载

Posted birkhoff

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了必应(http://cn.bing.com/)每日图片下载相关的知识,希望对你有一定的参考价值。

/**
 * Created on 2016年6月15日
 */
package com.bing;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author zhenggm<a href="mailto:[email protected]">zhenggm</a>
 * @version $Id$
 */
public class DownloadImg {
    public static void main(String[] args) {
        String url = "http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1";
        HttpClient client = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse response = client.execute(get);
            JSONObject json = JSON.parseObject(EntityUtils.toString(response.getEntity()));
            JSONArray obj = (JSONArray) JSONArray.parse(json.get("images").toString());
            String resourceUrl = obj.getJSONObject(0).get("url").toString();
            HttpGet request = new HttpGet(resourceUrl);
            HttpResponse resp = client.execute(request);
            if (resp.getStatusLine().getStatusCode() == 200) {
                InputStream in = resp.getEntity().getContent();
                byte[] b = new byte[1024];
                int len = 0;
                String path = "E:/testImage/bing/";
                File folder = new File(path);
                if (!folder.exists()) {
                    folder.mkdirs();
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                File f = new File(path + sdf.format(new Date()) + ".jpg");
                if (!f.exists()) {
                    f.createNewFile();
                }
                OutputStream out = new FileOutputStream(f);
                while ((len = in.read(b)) != -1) {
                    out.write(b, 0, len);
                }
                out.close();
                System.out.println("File is stored in: " + path);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

以上是关于必应(http://cn.bing.com/)每日图片下载的主要内容,如果未能解决你的问题,请参考以下文章

必应的两个壁纸接口,可用来获取海量的高质量壁纸和相关故事

必应每日壁纸下载|必应每日壁纸V3.3.1免费下载

我是如何白嫖 Github 服务器自动抓取每日必应壁纸的?

必应词典每日一句收藏

怎样保存必应每天自动更换的壁纸?

批处理脚本获取必应的每日图片