Java操作ftpClient常用方法

package com.common.service.impl;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
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.stereotype.Service;
import com.common.service.FtpService;
import com.pojo.FtpModel;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@Service
public class FtpServiceImpl implements FtpService {
ThreadLocal threadFtpClient = new ThreadLocal();

public FtpServiceImpl() {
super();
init();
}

private void init() {
log.info("初始化 ---------ftp链接数据---------");
}
        //获取ftp连接
public FTPClient getFTPClient() {
FTPClient ftpClient = threadFtpClient.get();
if (ftpClient == null) {
throw new RuntimeException("连接已被关闭!");
}
return ftpClient;
}
        
public void login(FtpModel ftpModel) throws IOException {
{
FTPClient ftpClient = null;
try {
ftpClient = new FTPClient();
ftpClient.setControlEncoding("GBK");
ftpClient.connect(ftpModel.getFtpHost(), ftpModel.getFtpPort());// 连接FTP服务器
ftpClient.login(ftpModel.getFtpUserName(), ftpModel.getFtpPassword());// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
log.info("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
} else {
log.info("FTP连接成功。");
threadFtpClient.set(ftpClient);
}
} catch (SocketException e) {
log.info("FTP的IP地址可能错误,请正确配置。");
throw new IOException(e.getMessage());
}
}
}
     
@Override
public void uploadFile(InputStream file, String fileName) throws IOException {
String Uuid = UUID.randomUUID().toString().replace("-", "");
String tmpFileName = Uuid + ".tmp";
FTPClient ftpClient = getFTPClient();
ftpClient.setBufferSize(1024);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(tmpFileName, file);
ftpClient.rename(tmpFileName, fileName);
}
        
public List ls() throws IOException {
FTPClient ftpClient = getFTPClient();
List fileList = new ArrayList<>();
FTPFile[] listDirectories = ftpClient.listFiles();
for (FTPFile ftpFile : listDirectories) {
String name = ftpFile.getName();
fileList.add(name);
}
return fileList;
}

@Override
public void download(String fileName, File localFile) throws IOException {
FileOutputStream os = new FileOutputStream(localFile);
try {
FTPClient ftpClient = getFTPClient();
ftpClient.retrieveFile(fileName, os);
} finally {
os.close();
}
}

@Override
public void delete(String fileName) throws IOException {
FTPClient ftpClient = getFTPClient();
ftpClient.deleteFile(fileName);
}

@Override
public void cd(String path) throws IOException {
// TODO Auto-generated method stub
FTPClient ftpClient = getFTPClient();
ftpClient.changeWorkingDirectory(path);
}

@Override
public void mv(String fileName, String path) throws IOException {
// TODO Auto-generated method stub
FTPClient ftpClient = getFTPClient();
path= path+ fileName;
boolean flag = ftpClient.rename(fileName, path);
log.info("mvFile结果:" + flag);
}

@Override
public void cpFile(String fileName, String cpFileName) throws IOException {
FTPClient ftpClient = getFTPClient();
InputStream is = null;
try {
// 设置以二进制流的方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 下载
is = ftpClient.retrieveFileStream(new String(fileName.getBytes("GBK"), "iso-8859-1"));
// 主动调用一次getReply()把接下来的226消费掉. 这样做是可以解决这个返回null问题
ftpClient.getReply();
if (is != null) {
ftpClient.storeFile(new String(cpFileName.getBytes("GBK"), "iso-8859-1"), is);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
is.close();
}
}
}

        

     @Override
public void logout() {
FTPClient ftpClient = null;
try {
ftpClient = getFTPClient();
if (ftpClient != null) {
ftpClient.logout();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.disconnect();
threadFtpClient.remove();
} catch (IOException e) {
log.error("删除ftp链接失败");
}
}
}
}

}

附上ftpClient文档地址:http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html

你可能感兴趣的:(Java操作ftpClient常用方法)