下载网页图片到本地
程序员文章站
2022-04-13 14:10:20
...
public class CopyImg {
public static String downLoadImags(String imgUrl) {
InputStream inputStream = null;
String newPath = "";
try {
String imgType = imgUrl.substring(imgUrl.lastIndexOf(".") + 1);
URL url = new URL(imgUrl);
URLConnection conn = url.openConnection();
inputStream = conn.getInputStream();
String uuid = UUID.randomUUID().toString();
newPath = "D:\\bannerimgfile\\" + uuid + "." + imgType;
FileUtils.copyToFile(inputStream, new File(newPath));
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(inputStream);
}
return newPath;
}
public static void main(String[] args) {
System.out.println(downLoadImags("https://avatar.csdn.net/4/9/8/3_qq_27922023.jpg"));
}
}