栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java类下载壁纸练习小代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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> list = (List>) json1.get("list");
            for (Map map : list) {
                try {
                    test49(map.get("url"),map.get("tag")+map.get("id"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678801.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号