Created
September 24, 2026 05:21
-
-
Save asdf913/3384c8a45dd97b379ea8bf2d2b592abf to your computer and use it in GitHub Desktop.
Java program to upload file via SFTP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <dependencies> | |
| <!--https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> | |
| <dependency> | |
| <groupId>org.apache.commons</groupId> | |
| <artifactId>commons-lang3</artifactId> | |
| <version>3.20.0</version> | |
| </dependency> | |
| <!--https://mvnrepository.com/artifact/io.github.toolfactory/narcissus--> | |
| <dependency> | |
| <groupId>io.github.toolfactory</groupId> | |
| <artifactId>narcissus</artifactId> | |
| <version>1.0.13</version> | |
| </dependency> | |
| <!-- https://mvnrepository.com/artifact/org.logevents/logevents --> | |
| <dependency> | |
| <groupId>org.logevents</groupId> | |
| <artifactId>logevents</artifactId> | |
| <version>0.5.9</version> | |
| </dependency> | |
| <!--https://mvnrepository.com/artifact/org.apache.sshd/sshd-core --> | |
| <dependency> | |
| <groupId>org.apache.sshd</groupId> | |
| <artifactId>sshd-core</artifactId> | |
| <version>2.19.0</version> | |
| </dependency> | |
| <!--https://mvnrepository.com/artifact/org.apache.sshd/sshd-sftp --> | |
| <dependency> | |
| <groupId>org.apache.sshd</groupId> | |
| <artifactId>sshd-sftp</artifactId> | |
| <version>2.19.0</version> | |
| </dependency> | |
| <!--https://mvnrepository.com/artifact/commons-io/commons-io --> | |
| <dependency> | |
| <groupId>commons-io</groupId> | |
| <artifactId>commons-io</artifactId> | |
| <version>2.22.0</version> | |
| </dependency> | |
| </dependencies> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.apache.sshd.client; | |
| import java.io.File; | |
| import java.io.FileInputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.OutputStream; | |
| import java.lang.reflect.Field; | |
| import java.lang.reflect.Member; | |
| import java.lang.reflect.Proxy; | |
| import java.util.Collection; | |
| import java.util.LinkedHashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.Map.Entry; | |
| import java.util.Objects; | |
| import java.util.function.BiPredicate; | |
| import java.util.function.Predicate; | |
| import java.util.stream.Collector; | |
| import java.util.stream.Collectors; | |
| import java.util.stream.Stream; | |
| import org.apache.commons.io.IOUtils; | |
| import org.apache.commons.lang3.ArrayUtils; | |
| import org.apache.commons.lang3.ObjectUtils; | |
| import org.apache.commons.lang3.StringUtils; | |
| import org.apache.commons.lang3.function.FailableBiConsumer; | |
| import org.apache.commons.lang3.function.FailableBiFunction; | |
| import org.apache.commons.lang3.function.FailableConsumer; | |
| import org.apache.commons.lang3.function.FailableFunction; | |
| import org.apache.commons.lang3.math.NumberUtils; | |
| import org.apache.commons.lang3.reflect.FieldUtils; | |
| import org.apache.commons.lang3.tuple.Pair; | |
| import org.apache.sshd.client.future.AuthFuture; | |
| import org.apache.sshd.client.future.ConnectFuture; | |
| import org.apache.sshd.client.keyverifier.AcceptAllServerKeyVerifier; | |
| import org.apache.sshd.client.keyverifier.ServerKeyVerifier; | |
| import org.apache.sshd.client.session.ClientSession; | |
| import org.apache.sshd.client.session.ClientSessionCreator; | |
| import org.apache.sshd.common.future.VerifiableFuture; | |
| import org.apache.sshd.common.session.Session; | |
| import org.apache.sshd.common.session.SessionHolder; | |
| import org.apache.sshd.sftp.client.SftpClient; | |
| import org.apache.sshd.sftp.client.SftpClientFactory; | |
| import io.github.toolfactory.narcissus.Narcissus; | |
| public class SftpBatch { | |
| public static void main(final String[] args) throws Exception { | |
| // | |
| final Map<String, String> map = toMap(args); | |
| // | |
| try (final SshClient sshClient = SshClient.setUpDefaultClient()) { | |
| // | |
| setServerKeyVerifier(sshClient, AcceptAllServerKeyVerifier.INSTANCE); | |
| // | |
| start(sshClient); | |
| // | |
| try (final ClientSession clientSession = testAndApply( | |
| (a, b) -> Boolean.logicalAnd(a != null, StringUtils.isNotEmpty(b)), get(map, "user"), | |
| get(map, "host"), | |
| (a, b) -> getSession(verify(connect(sshClient, a, b, NumberUtils.toInt(get(map, "port"), 22)))), | |
| null)) { | |
| // | |
| testAndAccept(Objects::nonNull, get(map, "password"), x -> addPasswordIdentity(clientSession, x)); | |
| // | |
| try (final SftpClient sftpClient = isSuccess(verify(auth(clientSession))) | |
| ? createSftpClient(SftpClientFactory.instance(), clientSession) | |
| : null; | |
| final InputStream is = testAndApply(Objects::nonNull, | |
| testAndApply(Objects::nonNull, get(map, "file"), File::new, null), FileInputStream::new, | |
| null); | |
| final OutputStream os = write(sftpClient, get(map, "path"))) { | |
| // | |
| testAndAccept((a, b) -> Boolean.logicalAnd(a != null, b != null), is, os, IOUtils::copy); | |
| // | |
| } // try | |
| // | |
| } // try | |
| // | |
| } // try | |
| // | |
| } | |
| private static <T, U, E extends Exception> void testAndAccept(final BiPredicate<T, U> predicate, final T t, | |
| final U u, final FailableBiConsumer<T, U, E> consumer) throws E { | |
| if (predicate != null && predicate.test(t, u) && consumer != null) { | |
| consumer.accept(t, u); | |
| } | |
| } | |
| private static OutputStream write(final SftpClient instance, final String path) throws IOException { | |
| return instance != null ? instance.write(path) : null; | |
| } | |
| private static void setServerKeyVerifier(final ClientAuthenticationManager instance, | |
| final ServerKeyVerifier serverKeyVerifier) { | |
| if (instance != null) { | |
| instance.setServerKeyVerifier(serverKeyVerifier); | |
| } | |
| } | |
| private static <T, U, R, E extends Exception> R testAndApply(final BiPredicate<T, U> predicate, final T t, | |
| final U u, final FailableBiFunction<T, U, R, E> functionTrue, | |
| final FailableBiFunction<T, U, R, E> functionFalse) throws E { | |
| return predicate != null && predicate.test(t, u) ? apply(functionTrue, t, u) : apply(functionFalse, t, u); | |
| } | |
| private static <T, U, R, E extends Exception> R apply(final FailableBiFunction<T, U, R, E> instance, final T t, | |
| final U u) throws E { | |
| return instance != null ? instance.apply(t, u) : null; | |
| } | |
| private static SftpClient createSftpClient(final SftpClientFactory instnace, final ClientSession session) | |
| throws IOException { | |
| return instnace != null ? instnace.createSftpClient(session) : null; | |
| } | |
| private static ConnectFuture connect(final ClientSessionCreator instance, final String username, final String host, | |
| final int port) throws IOException { | |
| return instance != null ? instance.connect(username, host, port) : null; | |
| } | |
| private static AuthFuture auth(final ClientSession instance) throws IOException { | |
| return instance != null ? instance.auth() : null; | |
| } | |
| private static <T, E extends Exception> void testAndAccept(final Predicate<T> predicate, final T value, | |
| final FailableConsumer<T, E> consumer) throws E { | |
| if (test(predicate, value)) { | |
| accept(consumer, value); | |
| } | |
| } | |
| private static <T, E extends Exception> void accept(final FailableConsumer<T, E> instance, final T value) throws E { | |
| if (instance != null) { | |
| instance.accept(value); | |
| } | |
| } | |
| private static void addPasswordIdentity(final ClientAuthenticationManager instance, final String password) { | |
| if (instance != null) { | |
| instance.addPasswordIdentity(password); | |
| } | |
| } | |
| private static void start(final SshClient instance) { | |
| // | |
| if (instance == null) { | |
| // | |
| return; | |
| // | |
| } // if | |
| // | |
| final Field field = testAndApply(x -> size(x) == 1, | |
| collect(filter( | |
| stream(testAndApply(Objects::nonNull, getClass(instance), FieldUtils::getAllFieldsList, null)), | |
| f -> Objects.equals(getName(f), "state")), Collectors.toList()), | |
| x -> get(x, 0), null); | |
| // | |
| if (field == null || (Narcissus.getField(instance, field)) != null) { | |
| // | |
| instance.start(); | |
| // | |
| } // if | |
| // | |
| } | |
| private static boolean isSuccess(final AuthFuture instance) { | |
| return instance != null && instance.isSuccess(); | |
| } | |
| private static <S extends Session> S getSession(final SessionHolder<S> instance) { | |
| return instance != null ? instance.getSession() : null; | |
| } | |
| private static <T> T verify(final VerifiableFuture<T> instnace) throws IOException { | |
| return instnace != null ? instnace.verify() : null; | |
| } | |
| private static <V> V get(final Map<?, V> instance, final Object key) { | |
| return instance != null ? instance.get(key) : null; | |
| } | |
| private static Map<String, String> toMap(final String... ss) { | |
| // | |
| Map<String, String> map = null; | |
| // | |
| Entry<String, String> entry = null; | |
| // | |
| for (int i = 0; i < length(ss); i++) { | |
| // | |
| if ((entry = toEntry(ArrayUtils.get(ss, i))) == null) { | |
| // | |
| continue; | |
| // | |
| } // if | |
| // | |
| put(map = ObjectUtils.getIfNull(map, LinkedHashMap::new), getKey(entry), getValue(entry)); | |
| // | |
| } // for | |
| // | |
| return map; | |
| // | |
| } | |
| private static Entry<String, String> toEntry(final String string) { | |
| // | |
| final Field field = testAndApply(x -> size(x) == 1, | |
| collect(filter( | |
| stream(testAndApply(Objects::nonNull, getClass(string), FieldUtils::getAllFieldsList, null)), | |
| f -> Objects.equals(getName(f), "value")), Collectors.toList()), | |
| x -> get(x, 0), null); | |
| // | |
| if (string != null && field != null && Narcissus.getField(string, field) == null) { | |
| // | |
| return null; | |
| // | |
| } // if | |
| // | |
| if (Objects.equals(string, "=")) { | |
| // | |
| return Pair.of("", ""); | |
| // | |
| } else if (string != null && string.length() == 2 && string.charAt(0) == '=') { | |
| // | |
| return Pair.of("", string.substring(1, string.length())); | |
| // | |
| } else if (string != null && string.length() == 2 && string.charAt(string.length() - 1) == '=') { | |
| // | |
| return Pair.of(string.substring(0, string.length() - 1), ""); | |
| // | |
| } else if (string != null && string.indexOf('=') >= 0 && string.indexOf('=') == string.lastIndexOf('=')) { | |
| // | |
| return Pair.of(StringUtils.substringBefore(string, '='), StringUtils.substringAfter(string, '=')); | |
| // | |
| } else if (string != null && string.length() > 2 && string.indexOf('=') != string.lastIndexOf('=')) { | |
| // | |
| return Pair.of(StringUtils.substring(string, 0, string.indexOf('=')), | |
| StringUtils.substring(string, string.indexOf('=') + 1)); | |
| // | |
| } // if | |
| // | |
| return null; | |
| // | |
| } | |
| private static int length(final Object[] instance) { | |
| return instance != null ? instance.length : 0; | |
| } | |
| private static <K> K getKey(final Entry<K, ?> instance) { | |
| return instance != null ? instance.getKey() : null; | |
| } | |
| private static <V> V getValue(final Entry<?, V> instance) { | |
| return instance != null ? instance.getValue() : null; | |
| } | |
| private static <K, V> void put(final Map<K, V> instance, final K key, final V value) { | |
| if (instance != null) { | |
| instance.put(key, value); | |
| } | |
| } | |
| private static int size(final Collection<?> instance) { | |
| return instance != null ? instance.size() : 0; | |
| } | |
| private static <E> E get(final List<E> instance, final int index) { | |
| return instance != null ? instance.get(index) : null; | |
| } | |
| private static String getName(final Member instance) { | |
| return instance != null ? instance.getName() : null; | |
| } | |
| private static <T, R, A> R collect(final Stream<T> instance, final Collector<? super T, A, R> collector) { | |
| return instance != null && (collector != null || Proxy.isProxyClass(getClass(instance))) | |
| ? instance.collect(collector) | |
| : null; | |
| } | |
| private static <T> Stream<T> filter(final Stream<T> instance, final Predicate<? super T> predicate) { | |
| return instance != null ? instance.filter(predicate) : instance; | |
| } | |
| private static <T> Stream<T> stream(final Collection<T> instance) { | |
| return instance != null ? instance.stream() : null; | |
| } | |
| private static Class<?> getClass(final Object instance) { | |
| return instance != null ? instance.getClass() : null; | |
| } | |
| private static <T, R, E extends Throwable> R testAndApply(final Predicate<T> predicate, final T value, | |
| final FailableFunction<T, R, E> functionTrue, final FailableFunction<T, R, E> functionFalse) throws E { | |
| return test(predicate, value) ? apply(functionTrue, value) : apply(functionFalse, value); | |
| } | |
| private static <T> boolean test(final Predicate<T> instance, final T value) { | |
| return instance != null && instance.test(value); | |
| } | |
| private static <T, R, E extends Throwable> R apply(final FailableFunction<T, R, E> instance, final T value) | |
| throws E { | |
| return instance != null ? instance.apply(value) : null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment