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