java 访问 ftp_JAVA进行ftp操作(读取、下载、删除、切换路径)

该代码实现了一个FTPUtil工具类,用于连接、下载、删除FTP服务器上的文件,并能递归遍历目录。类中包含初始化FTP连接、下载文件、删除文件、遍历目录等方法,使用了Apache Commons Net库进行FTP操作。
摘要由CSDN通过智能技术生成

package com.xgjk.cms.customer.service.service.util;

import com.xgjk.cms.customer.service.service.BaseService;

import com.xgjk.cms.customer.service.service.dto.HospitalLabelDTO;

import org.apache.commons.net.ftp.FTP;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPFile;

import org.apache.commons.net.ftp.FTPReply;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Service;

import java.io.*;

import java.net.MalformedURLException;

import java.nio.charset.Charset;

import java.util.ArrayList;

import java.util.List;

import java.util.StringTokenizer;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

/**

* @author lyd

* @date 2018/9/5 14:10

*/

@Service

public class FtpUtils extends BaseService {

//ftp服务器地址

@Value("${label.hostname}")

private String hostname;

//ftp服务器端口号默认为3379

@Value("${label.port}")

private Integer port;

//ftp登录账号

@Value("${label.username}")

private String username;

//ftp登录密码

@Value("${label.password}")

private String password;

private FTPClient ftpClient = null;

/**

* 初始化ftp服务器

*/

public void init() {

ftpClient = new FTPClient();

ftpClient.setControlEncoding("utf-8");

try {

System.out.println("connecting...ftp服务器:" + hostname + ":" + port);

ftpClient.connect(hostname, port); //连接ftp服务器

ftpClient.login(username, password); //登录ftp服务器

int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器

if (!FTPReply.isPositiveCompletion(replyCode)) {

System.out.println("connect failed...ftp服务器:" + hostname + ":" + port);

}

System.out.println("connect successfu...ftp服务器:" + hostname + ":" + port);

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 下载文件 *

*

* @param pathname FTP服务器文件目录 *

* @param filename 文件名称 *

* @param localpath 下载后的文件路径 *

* @return

*/

public boolean downloadFile(String pathname, String filename, String localpath) {

ftpClient.enterLocalPassiveMode();

boolean flag = false;

OutputStream os = null;

try {

logger.info("开始下载文件");

//切换FTP目录

ftpClient.changeWorkingDirectory(pathname);

FTPFile[] ftpFiles = ftpClient.listFiles();

for (FTPFile file : ftpFiles) {

if (filename.equalsIgnoreCase(file.getName())) {

File localFile = new File(localpath + file.getName());

os = new FileOutputStream(localFile);

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

flag = ftpClient.retrieveFile(file.getName(), os);

if (!flag) {

logger.info("下载文件失败");

} else {

logger.info("下载文件成功");

}

os.close();

}

}

ftpClient.logout();

} catch (Exception e) {

logger.info("下载文件失败" + e.getMessage());

e.printStackTrace();

} finally {

if (ftpClient.isConnected()) {

try {

ftpClient.disconnect();

} catch (IOException e) {

e.printStackTrace();

}

}

if (null != os) {

try {

os.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return flag;

}

/**

* 删除文件 *

*

* @param filename 要删除的文件名称 *

* @return

*/

public boolean deleteFile(String filename, String pathname) {

init();

ftpClient.enterLocalPassiveMode();

boolean flag = false;

try {

ftpClient.changeWorkingDirectory(pathname);

System.out.println("开始删除文件");

flag = ftpClient.deleteFile(pathname + filename);

ftpClient.logout();

System.out.println("删除文件成功");

} catch (Exception e) {

System.out.println("删除文件失败");

e.printStackTrace();

} finally {

if (ftpClient.isConnected()) {

try {

ftpClient.disconnect();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return flag;

}

/**

* 递归遍历出目录下面所有文件

*

* @param pathName 需要遍历的目录,必须以"/"开始和结束

* @throws IOException

*/

public void list(String pathName, List arFiles) throws IOException {

ftpClient.enterLocalPassiveMode();

if (pathName.startsWith("/") && pathName.endsWith("/")) {

String directory = pathName;

//更换目录到当前目录

this.ftpClient.changeWorkingDirectory(directory);

FTPFile[] files = this.ftpClient.listFiles();

for (FTPFile file : files) {

if (file.isFile()) {

arFiles.add(directory + file.getName());

} else if (file.isDirectory()) {

list(directory + file.getName() + "/", arFiles);

}

}

}

}

/**

* 递归遍历目录下面指定的文件名

*

* @param pathName 需要遍历的目录,必须以"/"开始和结束

* @param ext 文件的扩展名

* @throws IOException

*/

public void list(String pathName, String ext, List arFiles) throws IOException {

ftpClient.enterLocalPassiveMode();

if (pathName.startsWith("/") && pathName.endsWith("/")) {

String directory = pathName;

//更换目录到当前目录

this.ftpClient.changeWorkingDirectory(directory);

FTPFile[] files = this.ftpClient.listFiles();

for (FTPFile file : files) {

if (file.isFile()) {

if (file.getName().endsWith(ext)) {

arFiles.add(file.getName());

}

} else if (file.isDirectory()) {

list(directory + file.getName() + "/", ext, arFiles);

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值