一、URL類實現文件下載
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class DownloadUtil {
public static void download(String urlStr, String savePath) {
try {
URL url = new URL(urlStr);
URLConnection connection = url.openConnection();
InputStream input = new BufferedInputStream(connection.getInputStream());
FileOutputStream output = new FileOutputStream(savePath);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
URL類是Java自帶的一個類,可以通過它來獲取URL地址的輸入流,進而下載網路資源。
在該示例中,將通過URL類獲取到指定url下載的網路資源的輸入流,然後以位元組流的形式讀取網路資源,最後以文件輸出流的形式將網路資源保存到本地
二、Apache HttpClient實現文件下載
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class DownloadUtil2 {
public static void download(String url, String filePath) throws ClientProtocolException, IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
FileOutputStream outstream = new FileOutputStream(filePath);
entity.writeTo(outstream);
outstream.close();
}
} finally {
httpClient.close();
}
}
}
Apache HttpClient是Apache的一個開源組件,該組件通過API提供了一套處理HTTP請求的類和介面,致力於提供更好的性能,簡化API的使用和提供對HTTP標準的全面實現。
在該示例中,通過HttpClient發送HTTP請求,獲取需要下載文件的輸入流,用位元組數組讀取,並以文件輸出流的形式將網路資源保存到本地
三、Java NIO實現文件下載
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.net.URL;
import java.net.URLConnection;
public class DownloadUtil3 {
public static void download(String urlStr, String savePath) throws IOException {
URL url = new URL(urlStr);
URLConnection connection = url.openConnection();
ReadableByteChannel readableByteChannel = Channels.newChannel(connection.getInputStream());
FileOutputStream fileOutputStream = new FileOutputStream(savePath);
WritableByteChannel writableByteChannel = Channels.newChannel(fileOutputStream);
ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024);
while (readableByteChannel.read(byteBuffer) > 0) {
byteBuffer.flip();
writableByteChannel.write(byteBuffer);
byteBuffer.clear();
}
}
}
Java NIO,即Java New IO,是Java SE 1.4中引入的新API,一套完整的IO API,可以替代原有的Java IO API。NIO提供了非阻塞IO,即選擇器技術,該技術允許伺服器端使用單線程來處理多個並發連接,並處理多個請求。
在該示例中,首先通過URL類獲得需要下載文件的輸入流,然後通過Java NIO的FileChannel和ByteBuffer進行讀寫操作,最終保存到本地文件中。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/193649.html