在 Java 中,可以使用不同的方式来实现 FTP 文件上传。下面我将介绍两种常见的实现方式,包括使用 Apache Commons Net 库和使用 Spring Integration 库。
Apache Commons Net 库是一个流行的 Java 库,用于处理网络通信协议,包括 FTP 协议。以下是使用该库实现 FTP 文件上传的步骤:
添加依赖(Maven):
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.8.0</version>
</dependency>
编写上传代码:
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FTPUploader {
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String username = "your-username";
String password = "your-password";
String remotePath = "/upload/";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(username, password);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
File localFile = new File("local-file.txt");
String remoteFileName = "remote-file.txt";
try (FileInputStream inputStream = new FileInputStream(localFile)) {
boolean uploaded = ftpClient.storeFile(remotePath + remoteFileName, inputStream);
if (uploaded) {
System.out.println("File uploaded successfully.");
} else {
System.out.println("File upload failed.");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Spring Integration 库提供了对集成不同通信协议的支持,包括 FTP。它的使用相对更加高级和灵活。以下是使用该库实现 FTP 文件上传的步骤:
添加依赖(Maven):
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ftp</artifactId>
<version>5.5.2</version>
</dependency>
编写上传代码:
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.MessageChannels;
import org.springframework.integration.ftp.dsl.Ftp;
import org.springframework.messaging.MessageChannel;
import java.io.File;
@Configuration
@EnableIntegration
@IntegrationComponentScan
public class FTPUploader {
@Bean
public IntegrationFlow ftpUploadFlow() {
return IntegrationFlows.from("inputChannel")
.handle(Ftp.outboundAdapter(ftpSessionFactory())
.useTemporaryFileName(true)
.remoteDirectory("/upload"))
.get();
}
@Bean
public DefaultFtpSessionFactory ftpSessionFactory() {
DefaultFtpSessionFactory factory = new DefaultFtpSessionFactory();
factory.setHost("ftp.example.com");
factory.setPort(21);
factory.setUsername("your-username");
factory.setPassword("your-password");
return factory;
}
@Bean
public MessageChannel inputChannel() {
return MessageChannels.direct().get();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(FTPUploader.class);
MessageChannel channel = context.getBean("inputChannel", MessageChannel.class);
File localFile = new File("local-file.txt");
channel.send(MessageBuilder.withPayload(localFile).build());
context.close();
}
}
在这两种实现方式中,你需要根据实际情况替换服务器、用户名、密码、本地文件路径等信息。这两种方法都提供了上传文件到 FTP 服务器的功能,你可以根据你的项目需求和技术栈来选择其中一种。