直接看代码
private static void downImage(String url , String path) {
try {
URL url1 = new URL(url);
//创建连接
URLConnection urlConnection = url1.openConnection();
//获得inputStream
InputStream inputStream = urlConnection.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
byte[] bytes = new byte[1024];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int length;
while ((length = dataInputStream.read(bytes)) > 0){
byteArrayOutputStream.write(bytes , 0 , length);
}
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
fileOutputStream.write(byteArrayOutputStream.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
}
path路径没有判断,路径要存在,否则报错,可以根据需要添加相应逻辑