一、URL下載文件
import java.net.URL;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
public class FileDownloader {
public static void main(String[] args) throws Exception {
String fileUrl = "https://example.com/file.pdf";
URL url = new URL(fileUrl);
BufferedInputStream bis = new BufferedInputStream(url.openStream());
FileOutputStream fos = new FileOutputStream("/path/to/file.pdf");
byte[] buffer = new byte[1024];
int count = 0;
while ((count = bis.read(buffer, 0, 1024)) != -1) {
fos.write(buffer, 0, count);
}
fos.close();
bis.close();
}
}
在Java中下載文件的第一步是創建一個URL對象,用於指定文件的地址,然後創建一個BufferedInputStream對象,並將URL打開的流傳遞給它,從而獲取文件數據。同時,創建一個FileOutputStream對象用於將從URL讀取的數據保存在本地磁碟上。
然後,在while循環中,將從BufferedInputStream讀取的數據寫入FileOutputStream中,使用1024位元組的緩衝區數組進行緩存,直至BufferedInputStream不再返回數據。最後,關閉流對象。
二、添加進度條
import java.net.URL;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.swing.JProgressBar;
public class FileDownloadWithProgressBar extends Thread {
private String fileUrl;
private String savePath;
private JProgressBar progressBar;
public FileDownloadWithProgressBar(String fileUrl, String savePath, JProgressBar progressBar) {
this.fileUrl = fileUrl;
this.savePath = savePath;
this.progressBar = progressBar;
}
public void run() {
BufferedInputStream bis = null;
FileOutputStream fos = null;
try {
URL url = new URL(fileUrl);
bis = new BufferedInputStream(url.openStream());
fos = new FileOutputStream(savePath);
byte[] buffer = new byte[1024];
int count = 0;
int progress = 0;
long fileSize = url.openConnection().getContentLength();
while ((count = bis.read(buffer, 0, 1024)) != -1) {
fos.write(buffer, 0, count);
progress += count;
int percentCompleted = (int) ((progress * 100) / fileSize);
progressBar.setValue(percentCompleted);
Thread.sleep(10);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
為了顯示下載進度,我們可以通過創建一個帶有進度條的GUI應用程序來實現,添加一個JProgressBar實例用來顯示下載進度。在子線程中,循環讀取數據,並在每次循環中更新進度條。
三、使用多線程下載
import java.net.URL;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
public class MultiThreadFileDownload extends Thread {
private String fileUrl;
private String savePath;
private long fileSize;
private int startByte;
private int endByte;
public MultiThreadFileDownload(String fileUrl, String savePath, int startByte, int endByte) {
this.fileUrl = fileUrl;
this.savePath = savePath;
this.startByte = startByte;
this.endByte = endByte;
}
@Override
public void run() {
BufferedInputStream bis = null;
FileOutputStream fos = null;
try {
URL url = new URL(fileUrl);
bis = new BufferedInputStream(url.openStream());
fos = new FileOutputStream(savePath);
byte[] buffer = new byte[1024];
long count = 0;
int size = endByte - startByte + 1;
bis.skip(startByte);
while (count < size) {
int len = bis.read(buffer, 0, 1024);
if (len == -1) {
break;
}
fos.write(buffer, 0, len);
count += len;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
如果您需要加速下載過程,則可以使用多線程下載。每個線程將按照指定文件位元組範圍內的數據下載文件,並將其保存在本地磁碟上。然後,將所有線程下載的部分組合成完整的文件。使用多線程下載可以有效提高文件的下載速度。
原創文章,作者:NEJUM,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/313717.html