安装vsftp
Centos7.6 安装 FTP
导入依赖
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.8.0</version>
</dependency>
测试类
package com.platform.iot;
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 FtpClientTest {
public static void main(String[] args) throws IOException {
testFtpClient();
}
public static void testFtpClient() throws IOException {
FTPClient ftpClient = new FTPClient();
ftpClient.connect("yourip", 8081);
ftpClient.login("ftpdmin", "yourpassword");
ftpClient.enterLocalPassiveMode();
ftpClient.setControlEncoding("UTF-8");
String userPath = "/ftpdmin/upload";
String newPathname = "test";
FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\summergao\\Desktop\\redis.conf"));
boolean changeWorkingDirectory = ftpClient.changeWorkingDirectory(userPath);
System.out.println(changeWorkingDirectory);
ftpClient.makeDirectory(newPathname);
changeWorkingDirectory = ftpClient.changeWorkingDirectory(newPathname);
System.out.println(changeWorkingDirectory);
boolean setFileType = ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
System.out.println(setFileType);
boolean storeFile = ftpClient.storeFile("redis.conf", inputStream);
System.out.println(storeFile);
ftpClient.logout();
}
}