zoukankan      html  css  js  c++  java
  • JAVA 通过url下载图片保存到本地

        //java 通过url下载图片保存到本地  urlString 图片链接地址  imgName 图片名称
        public static void download(String urlString, String imgName) throws Exception {
            // 构造URL
            URL url = new URL(urlString);
            // 打开连接
            URLConnection con = url.openConnection();
            // 输入流
            InputStream is = con.getInputStream();
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流
            String filename = "D:\本地路径/" + imgName + ".jpg";  //本地路径及图片名称
            File file = new File(filename);
            FileOutputStream os = new FileOutputStream(file, true);
            // 开始读取
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
            System.out.println(imgName);
            // 完毕,关闭所有链接
            os.close();
            is.close();
        }
  • 相关阅读:
    UVa 727
    UVa 11495
    UVa 299
    UVa 10194
    UVa 146
    10025
    Tug of War POJ 2576 DP(类似背包)
    Problem A: Freckles UVA 10034 裸生成树
    UVA 562
    CF DIV 2 206 C. Vasya and Robot
  • 原文地址:https://www.cnblogs.com/lyso/p/14052889.html
Copyright © 2011-2022 走看看