zoukankan      html  css  js  c++  java
  • 下载网络文件到本地

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.net.URL;
    import java.util.List;
    import java.util.Map;
    
    public class DownloadImg {
    
        public static void main(String[] args) {
            String pbmAppUrl = "https://www.baidu.com/img/bd_logo1.png";
            download(pbmAppUrl);
    }

        /***
         * 下载图片
         * 
         * @param listImgSrc
         */
        private static void download(String url) {
            try {
                    String imageName = url.substring(url.lastIndexOf("/") + 1, url.length());
                    URL uri = new URL(url);
                    InputStream in = uri.openStream();
                    FileOutputStream fo = new FileOutputStream(new File(imageName));
                    byte[] buf = new byte[1024];
                    int length = 0;
                    System.out.println("开始下载:" + url);
                    while ((length = in.read(buf, 0, buf.length)) != -1) {
                        fo.write(buf, 0, length);
                    }
                    in.close();
                    fo.close();
                    System.out.println(imageName + "下载完成");
            } catch (Exception e) {
                System.out.println("下载失败");
            }
        }
    }
  • 相关阅读:
    二进制文件
    Python的特殊成员
    中标麒麟Linux7 如何关闭广播消息
    双重循环输出
    输出星期数
    九九乘法表
    打印菱形
    加法表
    求100以内所有偶数和
    猜大小
  • 原文地址:https://www.cnblogs.com/poterliu/p/12868415.html
Copyright © 2011-2022 走看看