I. Apache Commons Net: <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.6</version> </dependency> import org.apache.commons.net.ftp.FTPClient; public class FTPExample { public static void main(String[] args) { FTPClient ftpClient = new FTPClient(); try { ftpClient.connect("ftp.example.com"); ftpClient.login("username", "password"); ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } import org.apache.commons.net.ftp.FTPClient; public class FTPExample { public static void main(String[] args) { FTPClient ftpClient = new FTPClient(); try { ftpClient.connect("ftp.example.com"); ftpClient.login("username", "password"); FileInputStream fileInputStream = new FileInputStream(new File("file.txt")); ftpClient.storeFile("file.txt", fileInputStream); fileInputStream.close(); ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } II. Spring Integration: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-ftp</artifactId> </dependency> properties ftp.host=ftp.example.com ftp.port=21 ftp.username=username ftp.password=password <int:channel id="ftpChannel" /> <int:channel id="ftpResultChannel" /> <int-ftp:outbound-channel-adapter id="ftpOutbound" channel="ftpChannel" session-factory="ftpSessionFactory" remote-filename-generator-expression="'upload_' + new java.text.SimpleDateFormat('yyyyMMddHHmmssSSS').format(new java.util.Date()) + '.txt'" remote-directory="/upload" /> <bean id="ftpSessionFactory" class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory"> <property name="host" value="${ftp.host}" /> <property name="port" value="${ftp.port}" /> <property name="username" value="${ftp.username}" /> <property name="password" value="${ftp.password}" /> </bean> import org.springframework.messaging.Message; import org.springframework.messaging.support.GenericMessage; @Autowired private MessageChannel ftpChannel; public void uploadFile(String filePath) { File file = new File(filePath); Message<File> message = new GenericMessage<>(file); ftpChannel.send(message); }


上一篇:
下一篇:
切换中文