gpt4 book ai didi

java - 如何在java中使用HTTP url获取远程服务器目录的所有文件名?

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:04 25 4
gpt4 key购买 nike

我想通过 http URL 访问所有文件(名称)。前任。 http://app.examle.com/csv/所以我需要评估 csv 文件中存在的所有文件(名称)

     headerBaseUrl="http://app.examle.com/csv/stock.csv";
URL br = new URL(headerBaseUrl);
bufferedReader = new BufferedReader(
new InputStreamReader(br.openStream()));
// Create List for holding Employee objects
List<BranchWiseStock> branchWiseStock = new ArrayList<BranchWiseStock>();

String line = "";
// Read to skip the header
bufferedReader.readLine();
// Reading from the second line
while ((line = bufferedReader.readLine()) != null) {
String[] stockDetails = line.split(COMMA_DELIMITER);
if (stockDetails.length > 0) {
System.out.println(" data "+stockDetails);
}
}

在这里我可以轻松获取1个文件的数据但我想读取多个文件请帮助我...!

最佳答案

public class FTPDownloadFileDemo {
public static void main(String args[]) {

String hostname = "XXX";
String username = "XXX";
String password = "XXX";
//Single file download
// String copyFrom = "serverFolderPath/";
// String copyTo = "localPath/";
JSch jsch = new JSch();
Session session = null;
System.out.println("Trying to connect.....");
try {
session = jsch.getSession(username, hostname, 22);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");

session.setConfig(config);
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;

sftpChannel.cd("serverFolderPath/csv/");
Vector<ChannelSftp.LsEntry> list = sftpChannel.ls("*");
for(ChannelSftp.LsEntry entry : list) {
System.out.println("Name :"+entry.getFilename());
}
// sftpChannel.get(copyFrom, copyTo);
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
System.out.println("Done !!");
}
}

关于java - 如何在java中使用HTTP url获取远程服务器目录的所有文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42432158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com