Java发送GET请求下载文件

在Java中,我们可以使用HttpURLConnection类来发送GET请求并下载文件。HTTPURLConnection是Java标准库中用于发送HTTP请求的类之一。下面将介绍如何使用Java发送GET请求并下载文件的方法。

准备工作

在开始之前,我们需要导入Java的URL和HttpURLConnection类,这两个类分别位于java.netjava.net.HttpURLConnection包中。

import java.net.HttpURLConnection;
import java.net.URL;

发送GET请求

首先,我们需要创建一个URL对象,并指定要发送GET请求的目标URL。然后,我们使用创建的URL对象来创建一个HttpURLConnection对象。之后,我们可以设置连接的一些属性,例如超时时间、请求方法等。

URL url = new URL("
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// 设置请求方法为GET
connection.setRequestMethod("GET");

// 设置连接超时时间为5000毫秒
connection.setConnectTimeout(5000);

处理响应

一旦我们建立了连接并发送了GET请求,我们可以获取响应状态码,并根据状态码来判断请求是否成功。通常,状态码为200表示请求成功。

int responseCode = connection.getResponseCode();
if (responseCode == 200) {
    // 处理成功响应
} else {
    // 处理失败响应
}

如果请求成功,我们可以获取输入流来读取服务器返回的数据。在下载文件的情况下,我们可以将输入流写入本地文件。

InputStream inputStream = connection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream("downloaded_file.txt");

byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
    fileOutputStream.write(buffer, 0, bytesRead);
}

fileOutputStream.close();
bufferedInputStream.close();
inputStream.close();

完整的代码示例

下面是一个完整的代码示例,演示了如何使用Java发送GET请求并下载文件。

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class FileDownloader {
    public static void main(String[] args) {
        try {
            URL url = new URL("
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);

            int responseCode = connection.getResponseCode();
            if (responseCode == 200) {
                InputStream inputStream = connection.getInputStream();
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                FileOutputStream fileOutputStream = new FileOutputStream("downloaded_file.txt");

                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, bytesRead);
                }

                fileOutputStream.close();
                bufferedInputStream.close();
                inputStream.close();

                System.out.println("File downloaded successfully.");
            } else {
                System.out.println("Failed to download file. Response code: " + responseCode);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

类图

下面是类图,展示了FileDownloader类及其相关的类。

classDiagram
    FileDownloader ..|> HttpURLConnection
    FileDownloader ..|> URL
    FileDownloader -- InputStream
    InputStream <|-- BufferedInputStream
    FileDownloader -- FileOutputStream

流程图

下面是流程图,展示了发送GET请求并下载文件的流程。

flowchart TD
    A(创建URL对象) --> B(创建HttpURLConnection对象)
    B --> C(设置请求方法为GET)
    B --> D(设置连接超时时间)
    B --> E(获取响应状态码)
    E -- 请求成功 --> F(获取输入流)
    F --> G(写入本地文件)
    G --> H(关闭输入流和输出流)
    E -- 请求失败 --> I(处理失败响应)

以上就是使用Java发送GET请求并下载文件的方法。通过了解这些基本概念和代码示例,您可以在自己的项目中使用Java来下载文件。希望本文对您有所帮助!