public static void main(String[] args) throws Exception { /** * 第一种: * demo.jpg是要生成的文件名称 * FileUtils.copyURLToFile(new URL("https://pic.liesio.com/2020/09/09/0370dbef0898b.jpg"), new File("demo.jpg")); */ /** * 第二种: * demo.jpg是要生成的文件名称 */ URL url = new URL("https://pic.liesio.com/2020/09/09/0370dbef0898b.jpg"); //打开连接 URLConnection urlConnection = url.openConnection(); InputStream is = urlConnection.getInputStream(); FileOutputStream fos = new FileOutputStream("demo.jpg"); int len = 0; byte[] bytes = new byte[1024]; while ((len = is.read(bytes)) != -1) { fos.write(bytes, 0, len); fos.flush(); } fos.close(); is.close(); }