use of org.apache.commons.net.ftp.FTPClient in project spring-integration by spring-projects.
the class FtpRemoteFileTemplateTests method testINT3412AppendStatRmdir.
@Test
public void testINT3412AppendStatRmdir() throws IOException {
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
fileNameGenerator.setExpression("'foobar.txt'");
template.setFileNameGenerator(fileNameGenerator);
template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
template.setUseTemporaryFileName(false);
template.execute(session -> {
session.mkdir("foo/");
return session.mkdir("foo/bar/");
});
template.append(new GenericMessage<String>("foo"));
template.append(new GenericMessage<String>("bar"));
assertTrue(template.exists("foo/foobar.txt"));
template.executeWithClient((ClientCallbackWithoutResult<FTPClient>) client -> {
try {
FTPFile[] files = client.listFiles("foo/foobar.txt");
assertEquals(6, files[0].getSize());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
template.execute((SessionCallbackWithoutResult<FTPFile>) session -> {
assertTrue(session.remove("foo/foobar.txt"));
assertTrue(session.rmdir("foo/bar/"));
FTPFile[] files = session.list("foo/");
assertEquals(0, files.length);
assertTrue(session.rmdir("foo/"));
});
assertFalse(template.getSession().exists("foo"));
}
use of org.apache.commons.net.ftp.FTPClient in project spring-integration by spring-projects.
the class FtpOutboundGateway method doInWorkingDirectory.
private <V> V doInWorkingDirectory(Message<?> message, Session<FTPFile> session, Callable<V> task) throws IOException {
Expression workingDirExpression = this.workingDirExpression;
FTPClient ftpClient = (FTPClient) session.getClientInstance();
String currentWorkingDirectory = null;
boolean restoreWorkingDirectory = false;
try {
if (workingDirExpression != null) {
currentWorkingDirectory = ftpClient.printWorkingDirectory();
String newWorkingDirectory = workingDirExpression.getValue(this.evaluationContext, message, String.class);
if (!Objects.equals(currentWorkingDirectory, newWorkingDirectory)) {
ftpClient.changeWorkingDirectory(newWorkingDirectory);
restoreWorkingDirectory = true;
}
}
return task.call();
} catch (Exception e) {
if (e instanceof IOException) {
throw (IOException) e;
} else if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new IOException("Uncategorised IO exception", e);
}
} finally {
if (restoreWorkingDirectory) {
ftpClient.changeWorkingDirectory(currentWorkingDirectory);
}
}
}
use of org.apache.commons.net.ftp.FTPClient in project spring-integration by spring-projects.
the class FtpServerOutboundTests method testMGETOnNullDir.
@Test
@SuppressWarnings("unchecked")
public void testMGETOnNullDir() throws IOException {
Session<FTPFile> session = ftpSessionFactory.getSession();
((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
session.close();
this.inboundMGet.send(new GenericMessage<Object>(""));
Message<?> result = this.output.receive(1000);
assertNotNull(result);
List<File> localFiles = (List<File>) result.getPayload();
assertThat(localFiles.size(), Matchers.greaterThan(0));
for (File file : localFiles) {
assertThat(file.getName(), isOneOf(" localTarget1.txt", "localTarget2.txt"));
assertThat(file.getName(), not(containsString("null")));
}
}
use of org.apache.commons.net.ftp.FTPClient in project spring-integration by spring-projects.
the class FtpServerOutboundTests method testLsForNullDir.
@Test
@SuppressWarnings("unchecked")
public void testLsForNullDir() throws IOException {
Session<FTPFile> session = ftpSessionFactory.getSession();
((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
session.close();
this.inboundLs.send(new GenericMessage<String>("foo"));
Message<?> receive = this.output.receive(10000);
assertNotNull(receive);
assertThat(receive.getPayload(), instanceOf(List.class));
List<String> files = (List<String>) receive.getPayload();
assertEquals(2, files.size());
assertThat(files, containsInAnyOrder(" ftpSource1.txt", "ftpSource2.txt"));
FTPFile[] ftpFiles = ftpSessionFactory.getSession().list(null);
for (FTPFile ftpFile : ftpFiles) {
if (!ftpFile.isDirectory()) {
assertTrue(files.contains(ftpFile.getName()));
}
}
}
use of org.apache.commons.net.ftp.FTPClient in project commons-utils-core by jiayongming.
the class FTPUtilImpl method createFTPClien.
// 创建变连接FTP
private FTPClient createFTPClien(FTPVo vo) {
FTPClient client = new FTPClient();
int reply = -1;
try {
client.connect(vo.getHostName(), vo.getPort());
client.login(vo.getUsername(), vo.getPassword());
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
return null;
} else {
client.setControlEncoding(vo.getRemoteEncoding());
client.setFileType(FTPClient.BINARY_FILE_TYPE);
if (vo.isPassiveMode()) {
client.enterLocalPassiveMode();
} else {
client.enterRemotePassiveMode();
}
client.cwd(vo.getRemoteDir());
}
return client;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Aggregations