java - 如何将文本文件 (txt) 写入我的 ftp 服务器?
问题描述
我怎样才能做到这一点?请问谁能帮帮我?我尝试了所有的 Apache 插件,但它不起作用!你知道任何可以帮助我编写txt文件并将它们写入我的FTP服务器的插件吗?我的荣幸。(对不起,我的英语一点也不好听)
解决方案
首先,您不能直接在 FTP 上写入文件。您需要在本地编写它,然后将其存储在 FTP 上。试试这个,它对我有用。
public static void main(String[] args) {
String server = "ftp://ftp.dlptest.com/";
int port = 21;
String user = "dlpuser@dlptest.com";
String pass = "fLDScD4Ynth0p4OJ6bW6qCxjh";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
File firstLocalFile = new File("C:/Users/Administrator/Desktop/Book1.xlsx");
String firstRemoteFile = "Book1.xlsx";
InputStream inputStream = new FileInputStream(firstLocalFile);
System.out.println("Start uploading first file");
boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
inputStream.close();
if (done) {
System.out.println("The first file is uploaded successfully.");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
推荐阅读
- blazor - Blazor 客户端可以使用类库吗?
- javascript - 如何检查访问麦克风的权限是否已被 FireFox 中的用户拒绝
- rust - 在循环内访问向量的元素时遇到问题
- javascript - Chart.js - 缩小图例以适应数据
- reactjs - 在 ReactJS 中,当您在表单组件中选择某些内容时,如何更改所有项目的边框颜色?
- python - 用于列中具有特定值的行计数的新数据框列,具有相同的客户 ID 列且小于日期和时间
- excel - Tableau 正在添加多次出现的数字。我怎样才能解决这个问题?
- azure - 在天蓝色中设置 showPii
- c - 如何检查字符串中是否存在至少三个数字?
- r - 基于两列创建年龄变量