Apache FTP Client has several outstanding issues with this. Below are instructions on how to use Ftp4J to effectively handle ftp programatically though java.
Download Ftp4J: http://www.sauronsoftware.it/projects/ftp4j/download.php
Then in your IDE:
import java.io.File;
import java.io.IOException;
import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
public class FTP4J {
/**
* @param args
* @throws FTPAbortedException
* @throws FTPDataTransferException
* @throws FTPException
* @throws FTPIllegalReplyException
* @throws IOException
* @throws IllegalStateException
*/
public static void main(String[] args) throws IllegalStateException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException {
FTP4J ftp= new FTP4J();
ftp.transfer();
}
private void transfer() throws IllegalStateException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException{
FTPClient client = new FTPClient();
client.connect("192.168.0.1"); //conect to FTP server (in my case a vsftp on centos 6.4)
client.login("admn", "admn123");//login to FTP Server
client.changeDirectory("/usr/share/tomcat/webapps/imgs/"); //tell FTP4J where on the Ftp Server to send your file that you want to upload.
File fileUpload = new File ("C:\Users\ih8w8\Pictures\1.jpg"); //point FTP4J to the file you want to upload
client.upload(fileUpload); //upload it
client.disconnect(true); //close connection (note: you could also log out first, then disconn if youre not in a test env)
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…