sftp and ftp download files to the current service based on the configured remote server address
- 2020-05-12 02:38:03
- OfStack
Without further ado, the key code looks like this:
package com.eastrobot.remote;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.eastrobot.util.PropertiesUtil;
/**
* full.zhang
*
* ftp/sftp Abstract method class
*
*/
public abstract class FileRemote {
private static final String FTP_MODE = "ftp";
private static final String SFTP_MODE = "sftp";
public static String ftproot;
public static String mode;
public static String host;
public static String username;
public static String password;
public static String port;
private static FileRemote client = null;
// The biggest 1 Download times sex 50 A file
public static int max = 50;
private final static Log LOGGER = LogFactory.getLog(FileRemote.class);
public static FileRemote getInstance() {
if (client == null) {
ftproot = PropertiesUtil.getString("transfer.root");
mode = PropertiesUtil.getString("transfer.mode");
host = PropertiesUtil.getString("transfer.host");
username = PropertiesUtil.getString("transfer.username");
password = PropertiesUtil.getString("transfer.password");
port = PropertiesUtil.getString("transfer.port");
if (mode.equals(FTP_MODE)) {
client = new FileFtpRemote();
} else if (mode.equals(SFTP_MODE)) {
client = new FileSftpRemote();
}
}
return client;
}
/**
* Perform scheduled tasks
*/
public void process() {
LOGGER.debug("---------------------------------------- Enter timing to download remote files ");
// Creating a thread pool
ExecutorService exec = Executors.newSingleThreadExecutor();
exec.execute(new Runnable() {
@Override
public void run() {
// Establish a connection
initFtpInfo(host, port, username, password);
// The collection of active file paths for remote services
List<String> listSourcePath = listRemoteFilePath(ftproot);
if (listSourcePath.isEmpty()) {
LOGGER.debug("____________________ Release the connection ");
client.closeConnection();
return;
}
if (listSourcePath.size() > max) {
listSourcePath = listSourcePath.subList(0, max);
}
for (String path : listSourcePath) {
downloadRemoteFile(path);
}
LOGGER.debug("____________________ Release the connection ");
client.closeConnection();
}
});
exec.shutdown();
}
/**
* Initialize connection
*
* @param host
* @param port
* @param username
* @param password
* @throws Exception
* @return
*/
public abstract void initFtpInfo(String host, String port, String username, String password);
/**
* Download files from remote service to local service
*
* @param path
* @return
* @throws Exception
*/
public abstract void downloadRemoteFile(String filePath);
/**
* Gets a collection of all file paths in the specified directory under the remote service ( Contains files in subdirectories )
*
* @param path
* @return
*/
public abstract List<String> listRemoteFilePath(String path);
/**
* Release the connection
*/
public abstract void closeConnection();
}
[java] view plain copy
package com.eastrobot.remote;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import com.eastrobot.command.Commander;
public class FileFtpRemote extends FileRemote {
protected FTPClient ftpClient;
private String encoding = "UTF-8";
private boolean binaryTransfer = true;
private final static Log LOGGER = LogFactory.getLog(FileFtpRemote.class);
@Override
public void initFtpInfo(String host, String port, String username, String password) {
try {
// structure 1 a FtpClient The instance
ftpClient = new FTPClient();
// Set character set
ftpClient.setControlEncoding(encoding);
// The connection FTP The server
ftpClient.connect(host, StringUtils.isNotBlank(port) ? Integer.valueOf(port) : 21);
// The return code is detected after the connection to verify that the connection was successful
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
// Log in to ftp The server
if (ftpClient.login(username, password)) {
setFileType();
}
ftpClient.login(username, password);
} else {
ftpClient.disconnect();
LOGGER.error("ftp Denial of service connection !");
}
} catch (Exception e) {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect(); // disconnect
} catch (IOException e1) {
LOGGER.error("ftp The service connection failed to disconnect !");
}
}
LOGGER.error("ftp Service connection failed !");
}
}
/**
* Sets the file transfer type
*/
private void setFileType() {
try {
if (binaryTransfer) {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
} else {
ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void downloadRemoteFile(String filePath) {
if (StringUtils.endsWith(filePath, "/") || StringUtils.endsWith(filePath, File.separator)) {
filePath = filePath.substring(0, filePath.length() - 1);
}
File saveFile = new File(filePath);
if (saveFile.exists()) {
return;
}
// Directory where the files are located
String path = filePath.substring(0, filePath.lastIndexOf("/"));
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) {
if (Commander.isLinux) {
path = path + File.separator;
} else {
path = path + "/";
}
}
OutputStream output = null;
try {
// Create the destination file path
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
saveFile.createNewFile();
// Transferred to the FTP Server directory
ftpClient.changeWorkingDirectory(path);
output = new FileOutputStream(saveFile);
ftpClient.retrieveFile(filePath, output);
} catch (IOException e) {
LOGGER.debug(" file :" + filePath + "______________________ Download failed !");
e.printStackTrace();
} finally {
LOGGER.debug(" file :" + filePath + "______________________ Download successful !");
IOUtils.closeQuietly(output);
}
}
@Override
public List<String> listRemoteFilePath(String path) {
List<String> list = new ArrayList<String>();
try {
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) {
if (Commander.isLinux) {
path = path + File.separator;
} else {
path = path + "/";
}
}
boolean changedir = ftpClient.changeWorkingDirectory(path);
if (changedir) {
ftpClient.setControlEncoding(encoding);
FTPFile[] files = ftpClient.listFiles();
for (FTPFile file : files) {
if (list.size() >= max) {
break;
}
if (file.isDirectory()) {
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) {
if (Commander.isLinux) {
path = path + File.separator;
} else {
path = path + "/";
}
}
list.addAll(this.listRemoteFilePath(path + file.getName()));
} else if (changedir) {
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) {
if (Commander.isLinux) {
path = path + File.separator;
} else {
path = path + "/";
}
}
File saveFile = new File(path + file.getName());
if (!saveFile.exists()) {
list.add(path + file.getName());
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
@Override
public void closeConnection() {
if (ftpClient != null) {
try {
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
[java] view plain copy
package com.eastrobot.remote;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.eastrobot.command.Commander;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.ChannelSftp.LsEntry;
public class FileSftpRemote extends FileRemote {
protected Session session = null;
protected ChannelSftp channel = null;
private final static Log LOGGER = LogFactory.getLog(FileSftpRemote.class);
@Override
public void initFtpInfo(String host, String port, String username, String password) {
try {
JSch jsch = new JSch(); // create JSch object
session = jsch.getSession(username, host, StringUtils.isNotBlank(port) ? Integer.valueOf(port) : 22);
session.setPassword(password); // Set the password
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config); // for Session Object to set properties
session.setTimeout(60000); // Set up the timeout time
session.connect(); // through Session Link building
Channel chan = session.openChannel("sftp"); // Open the SFTP channel
chan.connect(); // To establish SFTP Channel connection
channel = (ChannelSftp) chan;
} catch (Exception e) {
LOGGER.error("sftp The connection fails ");
e.printStackTrace();
}
}
@Override
public void downloadRemoteFile(String filePath) {
if (StringUtils.endsWith(filePath, "/") || StringUtils.endsWith(filePath, File.separator)) {
filePath = filePath.substring(0, filePath.length() - 1);
}
File saveFile = new File(filePath);
FileOutputStream output = null;
try {
if (saveFile.exists()) {
return;
}
// Create the destination file path
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
saveFile.createNewFile();
// Directory where the files are located
String path = filePath.substring(0, filePath.lastIndexOf("/"));
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) {
if (Commander.isLinux) {
path = path + File.separator;
} else {
path = path + "/";
}
}
channel.cd(path);
channel.get(filePath, new FileOutputStream(saveFile));
LOGGER.debug(" File: " + filePath + "____________________________________________ Download successful !");
} catch (Exception e) {
LOGGER.debug(" File: " + filePath + "____________________________________________ Download failed !");
e.printStackTrace();
} finally {
IOUtils.closeQuietly(output);
}
}
@SuppressWarnings("unchecked")
@Override
public List<String> listRemoteFilePath(String path) {
List<String> list = new ArrayList<String>();
Vector<LsEntry> v = null;
try {
if (!StringUtils.endsWith(path, "/") && StringUtils.endsWith(path, File.separator)) {
path = path + File.separator;
}
v = channel.ls(path);
} catch (SftpException e) {
e.printStackTrace();
}
for (LsEntry lsEntry : v) {
if (list.size() >= max) {
break;
}
if (!".".equals(lsEntry.getFilename()) && !"..".equals(lsEntry.getFilename())) {
SftpATTRS attrs = lsEntry.getAttrs();
if (attrs.isDir()) {
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) {
if (Commander.isLinux) {
path = path + File.separator;
} else {
path = path + "/";
}
}
list.addAll(this.listRemoteFilePath(path + lsEntry.getFilename()));
} else {
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) {
if (Commander.isLinux) {
path = path + File.separator;
} else {
path = path + "/";
}
}
File saveFile = new File(path + lsEntry.getFilename());
if (!saveFile.exists()) {
list.add(path + lsEntry.getFilename());
}
}
}
}
return list;
}
@Override
public void closeConnection() {
try {
if (channel != null) {
channel.quit();
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public ChannelSftp getChannel() {
return channel;
}
public void setChannel(ChannelSftp channel) {
this.channel = channel;
}
}