wget下载http下指定路径下的文件及其子目录下所有文件

如下:

wget -r -np -nH --cut-dirs=1 http://example.com/some/dir/

其中 dir/ 后的 “/” 必不可少,否则会下载到some路径下的其他目录及文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Apache Commons Net 库中的 FTPClient 类来实现 Java FTP 递归下载文件的功能。下面是一个示例代码,可以下载 FTP 服务器上指定目录下的所有文件及其子目录下的所有文件文件夹: ```java import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FtpDownloader { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String username = "username"; String password = "password"; String remoteDirPath = "/path/to/remote/directory"; String localDirPath = "/path/to/local/directory"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(username, password); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); downloadDirectory(ftpClient, remoteDirPath, localDirPath); ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } private static void downloadDirectory(FTPClient ftpClient, String remoteDirPath, String localDirPath) throws IOException { FTPFile[] subFiles = ftpClient.listFiles(remoteDirPath); if (subFiles != null && subFiles.length > 0) { for (FTPFile ftpFile : subFiles) { String remoteFilePath = remoteDirPath + "/" + ftpFile.getName(); String localFilePath = localDirPath + "/" + ftpFile.getName(); if (ftpFile.isDirectory()) { // create the directory in local file system File localDir = new File(localFilePath); if (!localDir.exists()) { localDir.mkdir(); } // download the sub directory recursively downloadDirectory(ftpClient, remoteFilePath, localFilePath); } else { // download the file OutputStream outputStream = new FileOutputStream(localFilePath); ftpClient.retrieveFile(remoteFilePath, outputStream); outputStream.close(); } } } } } ``` 在上面的代码中,`downloadDirectory()` 方法递归地下载指定目录下的所有文件文件夹。如果遇到子目录,它会在本地文件系统中创建一个对应的目录,并递归下载子目录下的所有文件文件夹。如果遇到普通文件,它会将其下载到本地文件系统中。 你需要将代码中的 `server`,`port`,`username`,`password`,`remoteDirPath` 和 `localDirPath` 替换为你自己的 FTP 服务器地址、端口、用户名、密码、远程目录和本地目录

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值