java ftp 空指针异常

发布于 2024-11-07 23:20:11 字数 1272 浏览 0 评论 0原文

我有将文件上传到服务器的代码。

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

    import java.io.File;
    import java.io.IOException;
    import org.apache.commons.net.ftp.FTPFile;
    import java.io.FileInputStream;
    import java.net.SocketException;

    public class FtpConnectDemo {
        public static void main(String[] args) throws SocketException, IOException{
            FTPClient client = new FTPClient();
            FileInputStream fis = null;

            client.connect("ftp.someserver.co.uk",21);
            boolean login = client.login("[email protected]",
                    "mypassword");


            String filename = "E:/workbench j2ee/cPEP_UI/WebContent/engine.xml";
            client.storeFile(filename, fis);
            client.logout();
            fis.close();
        }
    }

当我运行这段代码时,它给了我这个错误:

Exception in thread "main" java.lang.NullPointerException
    at FtpConnectDemo.main(FtpConnectDemo.java:22)

用户名、密码、服务器名都正确。那怎么了? 我可以使用 telnet 连接到 FTP。 有什么想法吗?

编辑 1

好的,现在我在初始化 fis 时没有收到空指针异常。但我的文件还没有上传;可能是什么问题?

I have code to upload a file to a server.

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

    import java.io.File;
    import java.io.IOException;
    import org.apache.commons.net.ftp.FTPFile;
    import java.io.FileInputStream;
    import java.net.SocketException;

    public class FtpConnectDemo {
        public static void main(String[] args) throws SocketException, IOException{
            FTPClient client = new FTPClient();
            FileInputStream fis = null;

            client.connect("ftp.someserver.co.uk",21);
            boolean login = client.login("[email protected]",
                    "mypassword");


            String filename = "E:/workbench j2ee/cPEP_UI/WebContent/engine.xml";
            client.storeFile(filename, fis);
            client.logout();
            fis.close();
        }
    }

and when I run this code, it is giving me this error:

Exception in thread "main" java.lang.NullPointerException
    at FtpConnectDemo.main(FtpConnectDemo.java:22)

The username, password, servername are all right. What's wrong then?
I am able to connect to FTP using telnet.
Any ideas?

EDIT 1

OK, now I am not getting the nullpointer exception, as I initialized fis. But my file is not uploaded yet; what might be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不乱于心 2024-11-14 23:20:11

你永远不会实例化你的变量fis。我认为这是你的问题。

这会导致两个问题:

  • 您尝试将 null 存储为文件,第 20 行。这是由您正在使用的 Apache FTP 库处理的。
  • NullPointerException 第 22 行,当您尝试调用 close() 时。

另外,我想指出的另一件事是:第 20 行,当您调用 storeFile 时。您给出的路径是指向本地文件的路径。我认为你应该把远程文件路径放在这里。

最终代码应如下所示:

// ...

FileInputStream fis = new FileInputStream("E:/workbench j2ee/cPEP_UI/WebContent/engine.xml");

// ...

client.storeFile("engine.xml", fis);

// ...

You never instanciate your variable fis. I think that this is your problem here.

This cause two problems:

  • You try to store null as a file, line 20. This is handled by the Apache FTP library you are using.
  • NullPointerException line 22, when you try to call close().

Also, other thing I'd like to point out: line 20, when you are calling storeFile. The path you are giving is a path pointing to a local file. I think you should put the remote file path in here.

The final code should look like this:

// ...

FileInputStream fis = new FileInputStream("E:/workbench j2ee/cPEP_UI/WebContent/engine.xml");

// ...

client.storeFile("engine.xml", fis);

// ...
把昨日还给我 2024-11-14 23:20:11

丢失了

fis = new FileInputStream(new File(filename));

您在 client.storeFile() 之前

you are missing

fis = new FileInputStream(new File(filename));

before client.storeFile()

梦与时光遇 2024-11-14 23:20:11

看起来您的 fis 不能是 null 以外的任何内容(您只能使用 null 初始化它),因此 NullPointerException当您尝试对其执行 close() 时,这是很自然的。

Looks like your fis cannot be anything other than null (you only initialise it with null), so the NullPointerException is natural when you try to execute close() on it.

南渊 2024-11-14 23:20:11

client.storeFile 上的 sendFile 方法返回一个布尔值。我会首先检查该值,看看它是否失败或者可能只是导入到您不期望的地方。

客户端还有一个 getReplyCodegetReplyString 方法您可以用来查看服务器正在做什么。

The sendFile method on client.storeFile returns a boolean. I would first check that value to see if it is failing or maybe just importing to somewhere you're not expecting.

The client also has a getReplyCode and getReplyString methods which you could use to see what the server is up to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文