java 读取服务器上的文件内容

 //读取服务器上的文件内容
         String urlPath="http://img.elongstatic.com/mas/podDescription/input.json";
        // 统一资源
        URL url = new URL(urlPath);
        // 连接类的父类,抽象类
        URLConnection urlConnection = url.openConnection();
        // http的连接类
        HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
        //设置超时
        httpURLConnection.setConnectTimeout(1000*5);
        //设置请求方式,默认是GET
        httpURLConnection.setRequestMethod("POST");
        // 设置字符编码
        httpURLConnection.setRequestProperty("Charset", "UTF-8");
        // 打开到此 URL引用的资源的通信链接(如果尚未建立这样的连接)。
        httpURLConnection.connect();
        // 文件大小
        int fileLength = httpURLConnection.getContentLength();

        // 控制台打印文件大小
        System.out.println("您要下载的文件大小为:" + fileLength / (1024 * 1024) + "MB");

        // 建立链接从请求中获取数据
        URLConnection con = url.o
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用URLConnection类或者Apache HttpClient库来读取远程服务器文件列表。 1. 使用URLConnection类: 首先,创建一个URL对象,指定要访问的远程服务器地址。然后,使用URL对象的openConnection()方法打开一个连接,并转换为HttpURLConnection对象。接着,设置HTTP请求的方法(GET、POST等)、超时时间等属性,并连接到远程服务器。通过getInputStream()方法获取远程服务器的响应数据流,并使用BufferedReader逐行读取文件列表。 以下是示例代码: ``` import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class RemoteFileReader { public static void main(String[] args) { try { URL url = new URL("https://example.com/filelist"); // 替换为实际的远程服务器地址 HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); // 设置连接超时时间为5秒 connection.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); // 输出远程服务器文件列表 } reader.close(); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ``` 2. 使用Apache HttpClient库: 首先,引入Apache HttpClient库的相关依赖。然后,创建一个HttpClient对象,并使用HttpGet请求指定的远程服务器地址。通过调用execute()方法发送请求并获取响应。使用EntityUtils类将响应的实体转换为字符串,并解析获取文件列表信息。 以下是示例代码: ``` import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class RemoteFileReader { public static void main(String[] args) { try { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("https://example.com/filelist"); // 替换为实际的远程服务器地址 HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); String filelist = EntityUtils.toString(entity); // 获取文件列表字符串 System.out.println(filelist); // 输出远程服务器文件列表 httpClient.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 无论是使用URLConnection类还是Apache HttpClient库,需要替换示例代码中的远程服务器地址为实际的地址。在实际应用中,可能需要根据远程服务器的访问授权、协议、端口等设置相关参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值