java下载图片到本地,例如从网上下载图片,下载淘宝图片,下载百度图片等
java下载图片到本地,例如从网上下载图片,下载淘宝图片,下载百度图片。
直接上代码如下:
public static void download(String url,String saveFileName) throws Exception{
URL url1 = new URL(url);
URLConnection uc = url1.openConnection();
InputStream inputStream = null;
FileOutputStream out = null;
try {
inputStream = uc.getInputStream();
out = new FileOutputStream(saveFileName);
int j = 0;
while ((j = inputStream.read()) != -1) {
out.write(j);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(out!=null){
out.close();
}
if(inputStream!=null) {
inputStream.close();
}
}
}
main方法为:
public static void main(String[] args){
download("https://hbimg.huabanimg.com/38d7f27f25bc27342ec40ebc1337d074a244b59822f4b-f0S0oF_fw658", "D:\p\1\data\1_无_"+ UUID.randomUUID().toString()+".jpg");
}
还没有评论,来说两句吧...