你可以使用Java的URLConnection类来实现下载服务器静态资源的GET方法。以下是一个示例代码:

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

public class StaticResourceDownloader {
    public static void main(String[] args) {
        String ipAddress = "192.168.0.1";
        String filePath = "/path/to/file.txt";
        
        downloadStaticResource(ipAddress, filePath);
    }
    
    public static void downloadStaticResource(String ipAddress, String filePath) {
        try {
            String url = "http://" + ipAddress + filePath;
            URL resourceUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) resourceUrl.openConnection();
            connection.setRequestMethod("GET");
            
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                String saveFilePath = "downloaded_file.txt";
                
                FileOutputStream outputStream = new FileOutputStream(saveFilePath);
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                
                outputStream.close();
                inputStream.close();
                
                System.out.println("File downloaded successfully.");
            } else {
                System.out.println("Failed to download file. Response Code: " + responseCode);
            }
            
            connection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在上面的代码中,downloadStaticResource方法接收服务器的IP地址和文件路径作为参数。它会使用URLConnection打开与资源的连接,并发送GET请求。如果响应码为200(HTTP_OK),则将资源的内容读取并保存到本地文件中。你可以根据需要修改保存文件的路径和名称。

请注意,上述代码仅适用于下载服务器上的静态资源。如果需要下载非静态资源(例如动态生成的内容),则需要使用其他方法

使用java 写一个get方法下载服务器静态资源得方法 入参是IP下载得文件路径

原文地址: https://www.cveoy.top/t/topic/h1DQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录