Java已知图片路径下载图片到本地

时间:2021-06-03 18:04:25   收藏:0   阅读:0
 public static void main(String[] args)
    {
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        HttpURLConnection httpUrl = null;
        int size = 0;
        byte[] buf = new byte[1024];

        File file = new File("E:/" + "1.jpg");
        try
        {
            URL url = new URL("https://images.cnblogs.com/cnblogs_com/qq1445496485/1909145/o_2103030645361045.jpg");
            httpUrl = (HttpURLConnection)url.openConnection();
            httpUrl.connect();
            bis = new BufferedInputStream(httpUrl.getInputStream());
            fos = new FileOutputStream(file);
            while ((size = bis.read(buf)) != -1)
            {
                fos.write(buf, 0, size);
            }
            fos.flush();
            fos.close();
            bis.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!