91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

Java下載文件時如何設置請求頭信息

小樊
104
2024-10-10 01:44:43
欄目: 編程語言

在Java中,你可以使用HttpURLConnection或者Apache HttpClient來發送帶有自定義請求頭的HTTP請求以下載文件。這里我將為你提供兩種方法的示例:

方法1:使用HttpURLConnection

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownloadFileWithHttpURLConnection {
    public static void main(String[] args) {
        String fileUrl = "https://example.com/file.txt";
        String destinationFile = "downloaded_file.txt";

        try {
            downloadFile(fileUrl, destinationFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void downloadFile(String fileUrl, String destinationFile) throws IOException {
        URL url = new URL(fileUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 設置請求頭信息
        connection.setRequestProperty("User-Agent", "Mozilla/5.0");
        connection.setRequestProperty("Authorization", "Bearer your_access_token");

        // 設置下載文件
        connection.setDoOutput(true);
        try (InputStream inputStream = connection.getInputStream();
             OutputStream outputStream = new FileOutputStream(destinationFile)) {

            byte[] buffer = new byte[1024];
            int bytesRead;

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }

        // 斷開連接
        connection.disconnect();
    }
}

方法2:使用Apache HttpClient

首先,確保你已經添加了Apache HttpClient的依賴。如果你使用的是Maven,可以在pom.xml文件中添加以下依賴:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>

然后,你可以使用以下代碼下載文件并設置請求頭信息:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DownloadFileWithApacheHttpClient {
    public static void main(String[] args) {
        String fileUrl = "https://example.com/file.txt";
        String destinationFile = "downloaded_file.txt";

        try {
            downloadFile(fileUrl, destinationFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void downloadFile(String fileUrl, String destinationFile) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(fileUrl);

        // 設置請求頭信息
        httpGet.setHeader("User-Agent", "Mozilla/5.0");
        httpGet.setHeader("Authorization", "Bearer your_access_token");

        try (InputStream inputStream = httpClient.execute(httpGet).getEntity().getContent();
             OutputStream outputStream = new FileOutputStream(destinationFile)) {

            byte[] buffer = new byte[1024];
            int bytesRead;

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }

        httpClient.close();
    }
}

請注意,你需要將your_access_token替換為實際的訪問令牌。這兩種方法都可以實現下載文件并設置請求頭信息。你可以根據自己的需求選擇合適的方法。

0
申扎县| 时尚| 福泉市| 桦南县| 会东县| 灵武市| 沾益县| 文昌市| 桂林市| 嵊州市| 灵台县| 株洲县| 乌拉特中旗| 滨海县| 全州县| 福海县| 临沭县| 比如县| 克拉玛依市| 弥渡县| 油尖旺区| 伊金霍洛旗| 卓资县| 临汾市| 东港市| 青阳县| 泰顺县| 江西省| 古田县| 祁门县| 大同市| 甘泉县| 格尔木市| 壤塘县| 文登市| 虎林市| 西吉县| 广安市| 中阳县| 兴业县| 西藏|