您的位置:

JAVA实现文件下载功能:快速下载文件的方法!

一、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进行读写操作,最终保存到本地文件中。