java通过http url读取远程文件

java通过http url读取远程文件

try {
 URL url = new URL("http://localhost:8080/VWdummy/VW/VW0001R/tgd/ja/test.txt");
 InputStream is = url.openStream();
 BufferedInputStream bis = new BufferedInputStream(is);
 StringBuffer sb = new StringBuffer("");
 int len = 0;
 byte[] temp = new byte[1024];
 while ((len = bis.read(temp)) != -1) {
     sb.append(new String(temp, 0, len));
 }
 sb.toString();
} catch (IOException e) {
    e.printStackTrace();
}
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以通过URLURLConnection类来获取远程服务器上某个文件夹下的所有文件。具体步骤如下: 1. 构造URL对象,指定要访问远程文件夹的URL地址。 2. 打开URL连接,获取URLConnection对象。 3. 设置URLConnection对象的一些属性,如超时时间、请求头等。 4. 获取InputStream流,读取远程文件夹下的所有文件。 5. 遍历InputStream流,获取所有文件名。 6. 关闭连接和输入流。 下面是示例代码: ```java import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; public class GetRemoteFiles { public static void main(String[] args) throws Exception { String urlPath = "http://example.com/files/"; // 远程文件夹的URL地址 URL url = new URL(urlPath); URLConnection conn = url.openConnection(); conn.setConnectTimeout(5000); // 设置连接超时时间为5秒 conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); // 设置请求头 InputStream inputStream = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; List<String> fileList = new ArrayList<String>(); while ((line = reader.readLine()) != null) { if (line.contains("<a href")) { // 获取文件名 String fileName = line.substring(line.indexOf("\"") + 1, line.lastIndexOf("\"")); fileList.add(fileName); } } reader.close(); inputStream.close(); conn.disconnect(); for (String fileName : fileList) { System.out.println(fileName); // 打印文件名 } } } ``` 这段代码会输出远程文件夹下的所有文件名。需要注意的是,如果远程文件夹需要身份验证,可以在URLConnection对象中设置用户名和密码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值