您可以使用Apache FTPClient库来将内存中的文本写入FTP服务器上的文件。以下是一个使用FTPClient的Java代码示例:
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class FTPExample {
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String username = "your-username";
String password = "your-password";
FTPClient ftpClient = new FTPClient();
try {
// 连接到FTP服务器
ftpClient.connect(server, port);
ftpClient.login(username, password);
// 设置文件类型为二进制
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 将文本写入内存中
String text = "Hello, FTP Server!";
InputStream inputStream = new ByteArrayInputStream(text.getBytes());
// 将内存中的文本写入FTP服务器上的文件
ftpClient.storeFile("/path/to/remote/file.txt", inputStream);
// 断开与FTP服务器的连接
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
请确保将server
,port
,username
,password
和/path/to/remote/file.txt
替换为您实际的FTP服务器信息和文件路径。这段代码将连接到FTP服务器,并将文本写入指定的文件中。