FTP协议传输——文件备份和删除
package com.kun.sq.util;
import com.kun.entity.po.dev.DpDevFtpServerPo;
import com.kun.framework.crypto.cipher.DesCipher;
import com.kun.portal.webframe.message.WebSocketUtil;
import com.kun.sq.constant.ResponseCode;
import com.kun.sq.dto.FileServerDto;
import com.kun.sq.service.ILifeCycleService;
import com.kun.web2db.utils.BaseResult;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.socket.WebSocketSession;
import java.io.*;
import java.net.SocketException;
@Component
public class FTPUtil {
private static Logger logger = Logger.getLogger(FTPUtil.class);
@Autowired
private ILifeCycleService lifeCycleService;
public FTPClient getFTPClient(String ftpHost, int ftpPort,
String ftpUserName, String ftpPassword, WebSocketSession session) {
FTPClient ftp = null;
try {
ftpPassword = DesCipher.decrypt(ftpPassword);
ftp = new FTPClient();
ftp.connect(ftpHost, ftpPort);
ftp.login(ftpUserName, ftpPassword);
ftp.setConnectTimeout(50000);
ftp.setControlEncoding("UTF-8");
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
logger.info("未连接到FTP,用户名或密码错误");
WebSocketUtil.send(session, "未连接到FTP,用户名或密码错误");
ftp.disconnect();
} else {
logger.info("FTP连接成功");
WebSocketUtil.send(session, "FTP连接成功");
}
} catch (SocketException e) {
e.printStackTrace();
logger.info("FTP的IP地址可能错误,请正确配置");
WebSocketUtil.send(session, "FTP的IP地址可能错误,请正确配置");
} catch (IOException e) {
e.printStackTrace();
logger.info("FTP的端口错误,请正确配置");
WebSocketUtil.send(session, "FTP的端口错误,请正确配置");
}
return ftp;
}
public boolean closeFTP(FTPClient ftp) {
try {
ftp.logout();
} catch (Exception e) {
logger.error("FTP关闭失败");
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
logger.error("FTP关闭失败");
}
}
}
return false;
}
public boolean downLoadFTP(FTPClient ftp, String filePath, String fileName,
String downPath) {
boolean flag = false;
try {
ftp.changeWorkingDirectory(filePath);
ftp.enterLocalPassiveMode();
FTPFile[] files = ftp.listFiles();
for (FTPFile file : files) {
if (file.getName().equals(fileName)) {
File downFile = new File(downPath + File.separator
+ file.getName());
OutputStream out = new FileOutputStream(downFile);
flag = ftp.retrieveFile(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"), out);
out.flush();
out.close();
if (flag) {
logger.info("下载成功");
} else {
logger.error("下载失败");
}
}
}
} catch (Exception e) {
logger.error("下载失败");
}
return flag;
}
public boolean uploadFile(FTPClient ftp, String filePath, String ftpPath,WebSocketSession session) {
boolean flag = false;
InputStream in = null;
try {
ftp.enterLocalPassiveMode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
if (!ftp.changeWorkingDirectory(ftpPath)) {
ftp.makeDirectory(ftpPath);
}
ftp.changeWorkingDirectory(ftpPath);
File file = new File(filePath);
in = new FileInputStream(file);
String tempName = ftpPath + File.separator + file.getName();
flag = ftp.storeFile(new String(tempName.getBytes("UTF-8"), "ISO-8859-1"), in);
if (flag) {
logger.info("上传成功");
} else {
logger.error("上传失败");
}
} catch (Exception e) {
e.printStackTrace();
WebSocketUtil.send(session, String.format("备份失败:%s",e.getMessage()));
logger.error("上传失败");
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
WebSocketUtil.send(session, String.format("备份失败:%s",e.getMessage()));
}
}
return flag;
}
public boolean copyFile(FTPClient ftp, FileServerDto fileServerDto, WebSocketSession socketSession) {
boolean flag = false;
try {
String oldPath = fileServerDto.getOriginalPath();
String backPath = fileServerDto.getBackPath();
String fileName = fileServerDto.getFileName();
String fileWildcard = fileServerDto.getFileWildcard();
String cycle = fileServerDto.getCycle();
WebSocketUtil.send(socketSession, String.format("文件原始路径为:%s\n 文件名为:%s\n 文件备份路径为:%s\n", oldPath, fileName, backPath));
long days2DateStr = 0;
if (!StringUtils.isEmpty(cycle)) {
days2DateStr = TimeUtil.days2TimeStamp(Integer.parseInt(cycle));
}
String[] split = {};
if (!StringUtils.isEmpty(fileWildcard)) {
split = fileWildcard.split("\\.");
}
FTPClient ftpClient = null;
Boolean currentServer = fileServerDto.getCurrentServer();
if (!currentServer) {
String backServerId = fileServerDto.getBackServerId();
if (!StringUtils.isEmpty(backServerId)) {
WebSocketUtil.send(socketSession, String.format("此次备份为跨服务器备份,开始查询备份服务器信息,服务器id为:%s", backServerId));
BaseResult fileServerInfo = lifeCycleService.findFileServerInfo(backServerId);
if (ResponseCode.SUCCESS_CODE != fileServerInfo.getReCode()) {
WebSocketUtil.send(socketSession, String.format("备份服务器信息查询失败。失败信息为:%s", fileServerInfo.getReMsg()));
return false;
}
DpDevFtpServerPo serverInfo = (DpDevFtpServerPo) fileServerInfo.getReInfo();
String ip = serverInfo.getFtpIp();
int port = Integer.parseInt(serverInfo.getFtpPort());
String username = serverInfo.getUserName();
String password = serverInfo.getUserPwd();
ftpClient = getFTPClient(ip, port, username, password, socketSession);
}
}
ftp.changeWorkingDirectory(oldPath);
ftp.enterLocalPassiveMode();
FTPFile[] files = ftp.listFiles();
ByteArrayInputStream in = null;
ByteArrayOutputStream out = null;
if ("自定义".equals(fileName)) {
fileName = "";
}
for (FTPFile file : files) {
if (StringUtils.isEmpty(fileName) && split.length > 0 && file.getName().endsWith(split[1])) {
if (currentServer) {
out = new ByteArrayOutputStream();
ftp.retrieveFile(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"), out);
in = new ByteArrayInputStream(out.toByteArray());
ftp.makeDirectory(backPath);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
flag = ftp.storeFile(backPath + File.separator + (new String(file.getName().getBytes("UTF-8"), "ISO-8859-1")), in);
out.flush();
out.close();
in.close();
if (flag) {
logger.info("备份成功");
} else {
logger.error("备份失败");
}
} else {
boolean uploadResult = uploadFile(ftpClient, oldPath + "/" + file.getName(), backPath,socketSession);
if (uploadResult) {
logger.info("备份成功");
} else {
logger.info("备份失败");
}
}
}
if (file.getName().equals(fileName)) {
if (currentServer) {
out = new ByteArrayOutputStream();
ftp.retrieveFile(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"), out);
in = new ByteArrayInputStream(out.toByteArray());
ftp.makeDirectory(backPath);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
flag = ftp.storeFile(backPath + File.separator + (new String(file.getName().getBytes("UTF-8"), "ISO-8859-1")), in);
out.flush();
out.close();
in.close();
if (flag) {
logger.info("转存成功");
} else {
logger.error("复制失败");
}
} else {
boolean uploadResult = uploadFile(ftpClient, oldPath + "/" + file.getName(), backPath,socketSession);
if (uploadResult) {
logger.info("备份成功");
} else {
logger.info("备份失败");
}
}
}
long time = file.getTimestamp().getTime().getTime();
if (StringUtils.isEmpty(fileName) && split.length > 0 && file.getName().endsWith(split[1]) && days2DateStr != 0 && days2DateStr < time) {
if (currentServer) {
out = new ByteArrayOutputStream();
ftp.retrieveFile(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"), out);
in = new ByteArrayInputStream(out.toByteArray());
ftp.makeDirectory(backPath);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
flag = ftp.storeFile(backPath + File.separator + (new String(file.getName().getBytes("UTF-8"), "ISO-8859-1")), in);
out.flush();
out.close();
in.close();
if (flag) {
logger.info("备份成功");
} else {
logger.error("备份失败");
}
} else {
boolean uploadResult = uploadFile(ftpClient, oldPath + "/" + file.getName(), backPath,socketSession);
if (uploadResult) {
logger.info("备份成功");
} else {
logger.info("备份失败");
}
}
}
}
} catch (Exception e) {
logger.error("复制失败");
}
return flag;
}
public boolean moveFile(FTPClient ftp, String oldPath, String newPath) {
boolean flag = false;
try {
ftp.changeWorkingDirectory(oldPath);
ftp.enterLocalPassiveMode();
FTPFile[] files = ftp.listFiles();
if (!ftp.changeWorkingDirectory(newPath)) {
ftp.makeDirectory(newPath);
}
ftp.changeWorkingDirectory(oldPath);
for (FTPFile file : files) {
flag = ftp.rename(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"), newPath + File.separator + new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"));
if (flag) {
logger.info(file.getName() + "移动成功");
} else {
logger.error(file.getName() + "移动失败");
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("移动文件失败");
}
return flag;
}
public boolean deleteByFileName(FTPClient ftp, FileServerDto fileServerDto) {
boolean flag = false;
try {
String oldPath = fileServerDto.getOriginalPath();
String fileName = fileServerDto.getFileName();
String fileWildcard = fileServerDto.getFileWildcard();
String[] split = {};
if (!StringUtils.isEmpty(fileWildcard)) {
split = fileWildcard.split("\\.");
}
String cycle = fileServerDto.getCycle();
long days2DateStr = 0;
if (!StringUtils.isEmpty(cycle)) {
days2DateStr = TimeUtil.days2TimeStamp(Integer.parseInt(cycle));
}
ftp.changeWorkingDirectory(oldPath);
ftp.enterLocalPassiveMode();
FTPFile[] files = ftp.listFiles();
for (FTPFile file : files) {
if (split.length > 0 && file.getName().endsWith(split[1])) {
flag = ftp.deleteFile(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"));
continue;
}
long time = file.getTimestamp().getTime().getTime();
if (split.length > 0 && file.getName().endsWith(split[1]) && days2DateStr > 0 && days2DateStr < time) {
flag = ftp.deleteFile(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"));
continue;
}
if (fileName.equals(file.getName())) {
flag = ftp.deleteFile(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"));
continue;
}
}
} catch (Exception e) {
logger.error(e.getMessage());
}
return flag;
}
public boolean deleteByFolder(FTPClient ftp, String FtpFolder) {
boolean flag = false;
try {
ftp.changeWorkingDirectory(new String(FtpFolder.getBytes("UTF-8"), "ISO-8859-1"));
ftp.enterLocalPassiveMode();
FTPFile[] files = ftp.listFiles();
for (FTPFile file : files) {
if (file.isFile()) {
ftp.deleteFile(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"));
}
if (file.isDirectory()) {
String childPath = FtpFolder + File.separator + file.getName();
deleteByFolder(ftp, childPath);
}
}
flag = ftp.removeDirectory(new String(FtpFolder.getBytes("UTF-8"), "ISO-8859-1"));
if (flag) {
logger.info(FtpFolder + "文件夹删除成功");
} else {
logger.error(FtpFolder + "文件夹删除成功");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("删除失败");
}
return flag;
}
public boolean readFileByFolder(FTPClient ftp, String folderPath) {
boolean flage = false;
try {
ftp.changeWorkingDirectory(new String(folderPath.getBytes("UTF-8"), "ISO-8859-1"));
ftp.enterLocalPassiveMode();
FTPFile files[] = ftp.listFiles();
InputStream in = null;
BufferedReader reader = null;
for (FTPFile file : files) {
if (file.isFile()) {
String fileName = file.getName();
if (fileName.endsWith(".txt")) {
in = ftp.retrieveFileStream(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"));
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String temp;
StringBuffer buffer = new StringBuffer();
while ((temp = reader.readLine()) != null) {
buffer.append(temp);
}
if (reader != null) {
reader.close();
}
if (in != null) {
in.close();
}
ftp.completePendingCommand();
System.out.println(buffer.toString());
}
}
if (file.isDirectory()) {
String path = folderPath + File.separator + file.getName();
readFileByFolder(ftp, path);
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("文件解析失败");
}
return flage;
}
}
SFTP协议备份文件和删除文件
package com.kun.sq.util;
import com.jcraft.jsch.*;
import com.kun.framework.crypto.cipher.DesCipher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
@Component
public class SftpUtil {
private Logger logger = LoggerFactory.getLogger(SftpUtil.class);
public ChannelSftp connect(String host, int port, String username, String password) {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
System.out.println("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("Connected to " + host + ".");
} catch (Exception e) {
e.printStackTrace();
sftp = null;
}
return sftp;
}
public ChannelSftp connectSftp(Session sshSession) {
ChannelSftp sftp = null;
try {
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (Exception e) {
e.printStackTrace();
sftp = null;
}
return sftp;
}
public void exec(Session sshSession, String command) {
ChannelExec channelExec = null;
try {
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel = sshSession.openChannel("exec");
channelExec = (ChannelExec) channel;
channelExec.setCommand(command);
channelExec.connect();
} catch (Exception e) {
e.printStackTrace();
channelExec = null;
} finally {
channelExec.disconnect();
}
}
public Session getSession(String host, int port, String username, String password) {
Session sshSession = null;
try {
password = DesCipher.decrypt(password);
JSch jsch = new JSch();
jsch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);
System.out.println("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect(1500);
System.out.println("Session connected.");
System.out.println("Opening Channel.");
} catch (Exception e) {
e.printStackTrace();
}
return sshSession;
}
public String upload(String directory, String uploadFile, ChannelSftp sftp) {
try {
if (!directory.equals("")) {
sftp.cd(directory);
}
File file = new File(uploadFile);
sftp.put(new FileInputStream(file), file.getName());
System.out.println("上传完成");
sftp.disconnect();
sftp.getSession().disconnect();
return "传输成功";
} catch (Exception e) {
e.printStackTrace();
sftp.disconnect();
try {
sftp.getSession().disconnect();
} catch (JSchException e1) {
e1.printStackTrace();
}
return "传输失败,请检查配置!";
}
}
public void download(String directory, String downloadFile,
String saveFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
sftp.disconnect();
sftp.getSession().disconnect();
System.out.println("download ok,session closed");
} catch (Exception e) {
sftp.disconnect();
try {
sftp.getSession().disconnect();
} catch (JSchException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
public List<String> read(String directory, String readFile, ChannelSftp sftp, String charSetName) {
List<String> stringlist = new ArrayList<>();
InputStream inputStream = null;
try {
sftp.cd(directory);
inputStream = sftp.get(readFile);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charSetName));
String line = null;
line = br.readLine();
while (line != null) {
stringlist.add(line);
line = br.readLine();
}
br.close();
} catch (SftpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringlist;
}
public void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
e.printStackTrace();
}
}
public Vector listFiles(String directory, ChannelSftp sftp)
throws SftpException {
return sftp.ls(directory);
}
public void upload(String directory, String sftpFileName, InputStream input, ChannelSftp sftp) throws SftpException, JSchException {
try {
sftp.cd(directory);
} catch (SftpException e) {
logger.warn("directory is not exist");
sftp.mkdir(directory);
sftp.cd(directory);
}
sftp.put(input, sftpFileName);
logger.info(String.format(">>>>>>file:[%s] is upload successful<<<<<<", sftpFileName));
}
}