目錄
- 工具
- 代碼實作
- 測驗
工具
<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執行緒就行了,下次寫,

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/203704.html
標籤:python
上一篇:唯一分解定理一篇就夠了
