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?
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?
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 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);
// ...