1.pom文件引入commons-io工具包
<!--IO流文件操作工具包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
2.新建测试类
package com.zwd.demothread.project;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class TestFileUtilsThread extends Thread {
private String sourceFilePath;
private String targetFilePath;
public TestFileUtilsThread(String sourceFilePath, String targetFilePath) {
this.sourceFilePath = sourceFilePath;
this.targetFilePath = targetFilePath;
}
@Override
public void run() {
try {
FileUtils.copyURLToFile(new URL(sourceFilePath), new File(targetFilePath));
System.out.println("文件下载成功了---" + targetFilePath);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new TestFileUtilsThread("https://t7.baidu.com/it/u=4214840714,1181319136&fm=193&f=GIF", "1.png").start();
new TestFileUtilsThread("https://t7.baidu.com/it/u=4214840714,1181319136&fm=193&f=GIF", "2.png").start();
new TestFileUtilsThread("https://t7.baidu.com/it/u=4214840714,1181319136&fm=193&f=GIF", "3.png").start();
}
}
执行结果: 文件默认存放位置在项目根目录下:
|