public class download {
public static void main(String[] args) {
FileOutputStream fos = null;
InputStream inputStream = null;
HttpURLConnection urlConnection =null;
try {
URL url = new URL("https://t7.baidu.com/it/u=2604797219,1573897854&fm=193&f=GIF");
urlConnection = (HttpURLConnection) url.openConnection();
inputStream = urlConnection.getInputStream();
fos = new FileOutputStream(new File("d:\\test\\8888.jpg"));
//创建Buffer
byte[] buffer = new byte[1024];
//读取数据到Buffer
int read;
while ((read = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
urlConnection.disconnect();
}
}
}