目錄
- 工具
- 代碼實作
- 測驗
工具
<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"/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/204170.html
標籤:其他
上一篇:唯一分解定理一篇就夠了
