【发布时间】:2017-04-30 23:10:51
【问题描述】:
我正在尝试将文件上传到 FTP 服务器
正如我在这里找到的 How do you upload a file to an FTP server?,我有这个代码:
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("IP");
client.login("user", "pwd");
client.changeWorkingDirectory("/a/b/c/");
// Create an InputStream of the file to be uploaded
String filePath = file.getPath();
fis = new FileInputStream(filePath);
String fileName = file.getName();
// Store file to server
client.storeFile(fileName, fis);
client.logout();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
当我运行它时,文件会在预期的位置创建,但它是空的 (0 kb)
写作过程也需要相当多的时间……
我做错了什么?
【问题讨论】:
-
查找这段代码好不好: String filePath = file.getPath(); fis = new FileInputStream(filePath);你如何创建文件对象?它可能很慢,因为您的互联网连接速度很慢或您的 ftp 服务器速度很慢......可能是
-
任何错误/异常?
-
你可以使用独立的 FTP 客户端将相同的文件上传到相同的服务器和目录吗?
-
我添加了 System.out.println(file.length());并且我尝试传输的文件不是空的(991b)。这是一个简单的 .txt 文件。没有错误或异常。我可以使用 FileZilla 将文件移动到该位置而没有问题
-
我在这里发现 java FTP 上传创建空文件(JK Patel 的答案),添加 client.enterLocalPassiveMode();解决了我的问题