步骤 通过下载地址创建url对象连接到这个资源的http从获取输入流读取并写入关闭连接 具体代码 package Url; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class UrlDown { public static void main(String[] args) throws Exception { //1.通过下载地址创建url对象 URL url = new URL("https://i1.hdslb.com/bfs/face/08b322c8ac249d26319557af4955a56ac6a4b397.jpg@140w_140h_1c.webp"); //2.连接到这个资源的http HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); //3.从获取输入流 InputStream inputStream = urlConnection.getInputStream(); //4.读取并写入 byte[] buffer = new byte[1024]; int len; FileOutputStream fileOutputStream = new FileOutputStream(new File("3.jpg")); while((len=inputStream.read(buffer)) != -1){ fileOutputStream.write(buffer,0,len); } //5.关闭连接 fileOutputStream.close(); inputStream.close(); urlConnection.connect(); } }