import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPFile;
import java.io.*;
public class FTPUpload{
public static boolean uploadfile(String server,String username,String Password,String source_file_path,String dest_dir){
FTPClient ftp=new FTPClient();
try {
int reply;
ftp.connect(server);
ftp.login(username, Password);
System.out.println("Connected to " + server + ".");
System.out.print(ftp.getReplyString());
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
return false;
}
System.out.println("FTP server connected.");
InputStream input= new FileInputStream(source_file_path);
ftp.storeFile(dest_dir, input);
System.out.println( ftp.getReplyString() );
input.close();
ftp.logout();
} catch(Exception e) {
System.out.println("err");
e.printStackTrace();
return false;
} finally {
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(Exception ioe) {
}
}
}
return true;
}
public static void main(String[] args) {
FTPUpload upload = new FTPUpload();
try {
upload.uploadfile("192.168.0.210","muruganp","vm4snk","/home/media/Desktop/FTP Upload/data.doc","/fileserver/filesbackup/Emac/");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Am using the above code to upload a file named "data.doc" in the server location 192.168.0.210. The destination location of my server is fileserver/filesbackup/Emac/.
But I end up receiving the error "553 Could not create file" although the server gets connected successfully. I suspect that I am giving the destination format in a wrong way. Kindly let me know what has to be done to resolve the issue?
The problem is that you try to upload the file to a directory. You should rather specifiy the destination filename, not the destination directory.
Does it work when you try the same in another FTP client?
[Update]
Here is some (untested, since I don't have an FTP server) code that does the error handling better and in a shorter form.