Java下载文件时如何设置请求头信息

avatar
作者
筋斗云
阅读量:0

在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替换为实际的访问令牌。这两种方法都可以实现下载文件并设置请求头信息。你可以根据自己的需求选择合适的方法。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!