java - 文件未成功上传至 FTP

标签 java apache ftp apache-commons

我编写了一个独立的程序来将文件上传到 FTP 服务器。代码运行正常,但我在 FTP 上找不到该文件。这是代码

import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;


public class FTPDemo {

    public static void main(String[] args) {

        FTPClient ftp = new FTPClient();
        int reply;

        try {

            ftp.connect("ip address");
            ftp.login("username","password");
            reply = ftp.getReplyCode();

            if(FTPReply.isPositiveCompletion(reply)){
                System.out.println("Connected Success");
            }else {
                System.out.println("Connection Failed");
                ftp.disconnect();
            }

            FileInputStream fis = null;
            String filename = "demo.txt";
            fis = new FileInputStream("C:\\demo.txt");
            System.out.println("Is file stored: "+ftp.storeFile(filename,fis));
            fis.close();
            ftp.disconnect();
        } catch (SocketException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
}

文件是否已存储返回 false。可能是什么问题?

最佳答案

让我引用一下 FTPClient documentation :

The convention for all the FTP command methods in FTPClient is such that they either return a boolean value or some other value. The boolean methods return true on a successful completion reply from the FTP server and false on a reply resulting in an error condition or failure. The methods returning a value other than boolean return a value containing the higher level data produced by the FTP command, or null if a reply resulted in an error condition or failure. If you want to access the exact FTP reply code causing a success or failure, you must call getReplyCode after a success or failure.

换句话说,要了解失败的实际原因,您需要调用 ftp.getReplyCode() 并从那里开始工作。

关于java - 文件未成功上传至 FTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10279885/

相关文章:

php - HTTP 转发 PLAINTEXT 警告

ssh - ftp通过filezilla到google云机,无法实现

recursion - 使用 FTP 将文件递归地 PUT 到远程服务器

java - 将二进制数据反序列化为对象时出现 StackOverflowError

java - 如何在java中将 boolean 列添加到结果集中,然后将结果添加到jtable

java - ORM - 将 hibernate 与内存数据混合

java - 定义一个扩展java中类的属性

mysql - 基于每个用户限制对存储文件的访问

apache - 虚拟主机应该在 DNS 服务器中定义为 CNAME 吗?

python-3.x - python3 ftplib SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:645)')