java类下载壁纸练习小代码

public static String test65(int start) {
        BufferedReader reader =  null;
        StringBuffer sb = new StringBuffer();
        String initUrl = "http://bz.hzwdd.cn/api.php?cid=6&start="+start+"&count=10";
        URL url = URLUtil.url(initUrl);
        try {
            reader = new BufferedReader(new InputStreamReader(url.openStream(),"utf-8"));
            String temp = null;
            if ((temp= reader.readLine()) != null) {
                sb.append(temp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  sb.toString();
    }

  扫VX 领Java资料,前端,测试,python等等资料都有

这里就是下载的一个方法,字节流读一个写一个哈哈哈,真像我刚学java的时候使用的ps:这里的URLUtil使用的是hutool工具记得使用完关闭嗷

public static void test49(String urll,String fileName) throws IOException {
        URL url = URLUtil.url(urll);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        //设置超时间为3秒
        conn.setConnectTimeout(5 * 1000);
        //防止屏蔽程序抓取而返回403错误
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        //得到输入流
        InputStream inputStream = conn.getInputStream();
        //获取自己数组
        byte[] getData = readInputStream(inputStream);
        //文件保存位置
        File saveDir = new File("D:/test/test/");
        if (!saveDir.exists()) {
            saveDir.mkdir();
        }
        File file = new File(saveDir + File.separator + fileName+".jpg");
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(getData);
        if (fos != null) {
            fos.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
        System.out.println("info:" + url + " download success");
    }

这里就单独提出另一个方法获取字节 记得使用完关闭哦~
 

public static byte[] readInputStream(InputStream inputStream) throws IOException {
    byte[] buffer = new byte[1024];
    int len = 0;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    while ((len = inputStream.read(buffer)) != -1) {
        bos.write(buffer, 0, len);
    }
    bos.close();
    return bos.toByteArray();
}

然后就是亘古不变的main方法啦~  这里用的是阿里的fastjson
 

public static void main(String[] args) {
        String s = test65(0);
        System.out.println("s = " + s);
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONObject json1 = (JSONObject) jsonObject.get("data");
        int total_page = (int) json1.get("total_page");
        for (int i = 0; i < total_page; i++) {
            s = test65(i);
            System.out.println("s = " + s);
            jsonObject = JSONObject.parseObject(s);
            json1 = (JSONObject) jsonObject.get("data");
            List<Map<String,String>> list = (List<Map<String, String>>) json1.get("list");
            for (Map<String, String> map : list) {
                try {
                    test49(map.get("url"),map.get("tag")+map.get("id"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

  扫VX 领Java资料,前端,测试,python等等资料都有

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值