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("下载失败");
        }
    }
}

 

相关文章:

  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2021-08-07
  • 2021-04-03
  • 2021-11-29
  • 2021-12-23
猜你喜欢
  • 2021-06-22
  • 2022-12-23
  • 2021-10-24
  • 2021-09-27
  • 2021-09-08
  • 2021-12-01
  • 2022-12-23
相关资源
相似解决方案