从Java中的URL获取文件夹列表可以通过以下步骤实现:
以下是一个示例代码:
import java.io.BufferedReader;
import java.io.IOException;
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 FolderListFromURL {
public static void main(String[] args) {
String urlString = "http://example.com/folder/"; // 替换为实际的URL地址
try {
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
List<String> folderList = new ArrayList<>();
while ((line = reader.readLine()) != null) {
if (line.contains("href=\"") && line.contains("/")) {
String folderName = line.substring(line.indexOf("\"") + 1, line.lastIndexOf("/"));
folderList.add(folderName);
}
}
reader.close();
inputStream.close();
for (String folder : folderList) {
System.out.println(folder);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
这段代码通过解析URL的响应内容,提取出所有文件夹的名称,并将其存储在一个列表中。你可以根据实际情况进行进一步的处理,比如输出到控制台或存储到数据库中。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云