java下载图片到本地实例
- 摘要:publicstaticvoidmain(String[]args){StringImageUrl="xxxxxxxxx";//图片urlStringpath="F://createVerifyCode.jpg";//保存地址downloadPic(ImageUrl,path);}//链接url下载图片publicstaticbooleandownloadPic(StringurlList,Stringpath){URLurl=null;try{url=newURL(urlList)
- 标签:图片 Java 下载 实例
public static void main(String[] args) {
String ImageUrl = "xxxxxxxxx";//图片url
String path="F://createVerifyCode.jpg";// 保存地址
downloadPic(ImageUrl ,path);
}
//链接url下载图片
public static boolean downloadPic(String urlList,String path) {
URL url = null;
try {
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}