首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何从Java登录和下载https网页中的文件?

要从Java中登录并下载HTTPS网页中的文件,您可以使用Java的HttpURLConnection或Apache HttpClient库。以下是一个使用HttpURLConnection的简单示例:

代码语言:java
复制
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownloadFile {
    public static void main(String[] args) {
        try {
            String url = "https://example.com/file.txt";
            String username = "your_username";
            String password = "your_password";
            String fileName = "output.txt";

            downloadFile(url, username, password, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void downloadFile(String urlStr, String username, String password, String fileName) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);

        // 设置用户名和密码
        String userPassword = username + ":" + password;
        String encodedAuthorization = Base64.getEncoder().encodeToString(userPassword.getBytes());
        connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = new BufferedInputStream(connection.getInputStream());
            FileOutputStream outputStream = new FileOutputStream(fileName);

            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }

            inputStream.close();
            outputStream.close();
        } else {
            System.out.println("Error: " + responseCode);
        }
    }
}

这个示例中,我们使用了HttpURLConnection连接到HTTPS网页,并设置了用户名和密码。然后,我们将文件保存到本地文件系统。

注意:这个示例中使用的Base64类是Java 8中的java.util.Base64。如果您使用的是其他版本的Java,可能需要导入不同的Base64类。

您还可以使用Apache HttpClient库来实现相同的功能。这是一个使用Apache HttpClient的示例:

代码语言:java
复制
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

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

public class DownloadFile {
    public static void main(String[] args) {
        try {
            String url = "https://example.com/file.txt";
            String username = "your_username";
            String password = "your_password";
            String fileName = "output.txt";

            downloadFile(url, username, password, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void downloadFile(String urlStr, String username, String password, String fileName) throws IOException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(urlStr);

        // 设置用户名和密码
        String userPassword = username + ":" + password;
        String encodedAuthorization = Base64.getEncoder().encodeToString(userPassword.getBytes());
        httpGet.setHeader("Authorization", "Basic " + encodedAuthorization);

        HttpResponse response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            byte[] content = EntityUtils.toByteArray(entity);

            FileOutputStream outputStream = new FileOutputStream(fileName);
            outputStream.write(content);
            outputStream.close();
        } else {
            System.out.println("Error: " + response.getStatusLine().getStatusCode());
        }
    }
}

这个示例中,我们使用了Apache HttpClient连接到HTTPS网页,并设置了用户名和密码。然后,我们将文件保存到本地文件系统。

注意:这个示例中使用的Base64类是Java 8中的java.util.Base64。如果您使用的是其他版本的Java,可能需要导入不同的Base64类。

无论您选择哪种方法,都需要确保您的Java项目已经正确导入所需的库。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券