目录
- 工具
- 代码实现
- 测试
工具
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version></dependency>
或者
https://mvnrepository.com/artifact/commons-io/commons-io/2.7
然后放入你的项目下
代码实现
public class WebDownloader {/*** 下载文件 (歌曲,图片,视频)* @param url 文件的http路径* @param fileName 保存的文件路径*/public void downloader(String url, String fileName) {try {FileUtils.copyURLToFile(new URL(url), new File(fileName));} catch (IOException e) {e.printStackTrace();}}
}
测试
public class Demo {private String url;private String fileName;public Demo(String url, String fileName) {this.url = url;this.fileName = fileName;}public void run() {// 开启下载WebDownloader webDownloader = new WebDownloader();webDownloader.downloader(url, fileName);System.out.println("下载了:" + fileName);}public static void main(String[] args) {Demo t1 = new Demo("https://cdn.cai-cai.me/x/851B1C9F-31E7-4B4C-B61B-E5348E06D9C1.jpg", "1.jpg");Demo t2 = new Demo("https://cdn.cai-cai.me/x/25BCB4F7-B969-48A6-9D95-0B5C4E2E3C5B.jpg", "2.jpg");Demo t3 = new Demo("https://cdn.cai-cai.me/x/15DF6B53-573B-4BF2-86E4-1C76119CF804.jpg", "3.jpg");t1.run();t2.run();t3.run();}}class WebDownloader {/*** 下载文件 (歌曲,图片,视频)* @param url 文件的http路径* @param fileName 保存的文件路径*/public void downloader(String url, String fileName) {try {FileUtils.copyURLToFile(new URL(url), new File(fileName));} catch (IOException e) {e.printStackTrace();}}
}
本来想写多线程的,main线程就行了。下次写。