When I try to upload a simple text file with the Apache Commons FTPClient and this code:
FTPClient ftp = new FTPClient();
int reply;
// connect
try {
ftp.connect(serverAdd);
ftp.login(username,password);
reply = ftp.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)){
System.out.println("Connected Success..");
// upload file
try {
String fileDir = "testfile.txt";
FileInputStream in = null;
in = new FileInputStream(fileDir);
ftp.storeFile(fileDir,in);
System.out.println("File upload complete..");
}catch(IOException e){
System.out.println(e);
}
ftp.disconnect();
System.out.println("Disconnected..");
}else {
System.out.println("Connection Failed..");
ftp.disconnect();
}
} catch (SocketException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
A file gets created in the root of the FTP server but it is empty. What's wrong? I already tried to change the ftp mode to BINARY when uploading a PDF file. But the file is also 0 in size.
ftp.setFileType(FTP.BINARY_FILE_TYPE);
I also only want to upload a bunch of txt files, so the default ascii mode should be fine, right?
ok, it seems that it is a probem with my firewall. when I deactivate the firewall the file gets written to the ftp with no problem.
I was having the same problem. The file created but size 0 KB. After setting mode to passive, my file successfully transferred to FTP server. In fact there are two things we have to take care while uploading file on FTP server.
set file type to binary
objFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
set mode to passive:
objFtpClient.enterLocalPassiveMode();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With