SftpService.java
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SftpService {
private static final String SFTP_ERRMSG_NO_SUCH_FILE = "No such file"; //Cela dépend-il de l'environnement?
private Session session = null;
private ChannelSftp channelSftp = null;
public SftpService(String host, int port, String username, String password) throws Exception {
log.info("Relier(host={}, port={}, user={})", host, port, username);
session = (new JSch()).getSession(username, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
}
public void cd(String path) throws Exception {
log.info("{}Accédez au répertoire.", path);
channelSftp.cd(path);
}
public List<String> ls(String path) throws Exception {
@SuppressWarnings({ "unchecked" })
Vector<LsEntry> lsEntries = channelSftp.ls(path);
List<String> filenames = new ArrayList<>();
for (LsEntry lsEntry : lsEntries) {
filenames.add(lsEntry.getFilename());
}
return filenames;
}
public InputStream get(String path) throws Exception {
log.info("{}Obtenir.", path);
return channelSftp.get(path);
}
public void put(String localPath, String remotePath) throws Exception {
log.info("{}À{}Mettre", localPath, remotePath);
channelSftp.put(localPath, remotePath);
}
public void mkdir(String path) throws Exception {
log.info("{}Créez un répertoire.", path);
channelSftp.mkdir(path);
}
public void rmdir(String path) throws Exception {
log.info("{}Supprimez le répertoire.", path);
channelSftp.rmdir(path);
}
public void mkdirIfNotExists(String path) throws Exception {
if (!isExists(path)) {
mkdir(path);
}
}
public void rmdirIfExists(String path) throws Exception {
if (isExists(path)) {
rmdir(path);
}
}
public boolean isExists(String path) throws Exception {
try {
channelSftp.ls(path);
} catch (SftpException e) {
if (SFTP_ERRMSG_NO_SUCH_FILE.equals(e.getMessage())) {
return false;
} else {
throw e;
}
}
return true;
}
public void rename(String fromPath, String toPath) throws Exception {
log.info("{}À{}Renommez en.", fromPath, toPath);
channelSftp.rename(fromPath, toPath);
}
public void disconnect() {
if (channelSftp != null || session != null) {
log.info("Déconnecter");
}
if (channelSftp != null && channelSftp.isConnected()) {
channelSftp.disconnect();
}
if (session != null && session.isConnected()) {
session.disconnect();
}
channelSftp = null;
session = null;
}
}
XXX.java
SftpService sftpService = null;
try {
sftpService = new SftpService(host, port, username, password);
sftpService.cd(targetDir);
List<String> filenames = sftpService.ls("*.csv");
for (String filename : filenames) {
try (InputStream inputStream = sftpService.get(filename)) {
// do something
}
}
} finally {
if (sftpService != null) {
sftpService.disconnect();
}
}
Recommended Posts