前言

使用java.net包下的URL类和common-io包下的FileUtils工具类

1.添加common-io依赖

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

2.案例代码

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.net.URL;

public class TestDownloadFile {

public static void main(String[] args) throws Exception{
String urlStr = "https://img2018.s.com/i-beta/1278703/201911/1278703-20191128121650595-812419505.png";
URL url = new URL(urlStr);
String tempFileName = "E://a.png";
File temp = new File(tempFileName);
FileUtils.copyURLToFile(url, temp);
}
}