java ftp 空指针异常
我有将文件上传到服务器的代码。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你永远不会实例化你的变量
fis
。我认为这是你的问题。这会导致两个问题:
null
存储为文件,第 20 行。这是由您正在使用的 Apache FTP 库处理的。NullPointerException
第 22 行,当您尝试调用close()
时。另外,我想指出的另一件事是:第 20 行,当您调用
storeFile
时。您给出的路径是指向本地文件的路径。我认为你应该把远程文件路径放在这里。最终代码应如下所示:
You never instanciate your variable
fis
. I think that this is your problem here.This cause two problems:
null
as a file, line 20. This is handled by the Apache FTP library you are using.NullPointerException
line 22, when you try to callclose()
.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:
丢失了
您在
client.storeFile()
之前you are missing
before
client.storeFile()
看起来您的
fis
不能是null
以外的任何内容(您只能使用null
初始化它),因此NullPointerException
当您尝试对其执行close()
时,这是很自然的。Looks like your
fis
cannot be anything other thannull
(you only initialise it withnull
), so theNullPointerException
is natural when you try to executeclose()
on it.client.storeFile 上的 sendFile 方法返回一个布尔值。我会首先检查该值,看看它是否失败或者可能只是导入到您不期望的地方。
客户端还有一个 getReplyCode 和 getReplyString 方法您可以用来查看服务器正在做什么。
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.