java 判断ftp文件是否存在_java中直接判断ftp上文件夹下是否存在某文件的方法

该博客介绍了如何在Java中通过非遍历FTP目录的方式来检查文件是否存在,利用DOS命令实现FTP文件下载,提高效率。代码示例展示了创建BAT文件执行FTP下载任务,并检查文件是否成功下载到本地。
摘要由CSDN通过智能技术生成

package com.wyebd.task.receive;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Date;

import com.wyebd.publicuse.ApplicationResource;

public class FtpDownload {

/**

* @description:以非遍历ftp目录的方式下载文件,通过dos命令的方式直接进行文件下载,加快了下载速度

* @author RickyLee

* @time:2013-06-05 10:00

*/

// ftp地址

private String ftp1Ip = null;

// ftp用户名

private String ftp2User = null;

// ftp密码

private String ftp3Pass = null;

// ftp上文件存放的路径

private String ftp4Dir = null;

// ftp上的文件名

private String ftp5FileName = null;

// 存放到本地的文件的全路径

private String localFullFilePath = null;

public String getFtp1Ip() {

  • 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、付费专栏及课程。

余额充值