Getting an error while FTP file using FTPClient

2019-07-10 08:00发布

问题:

I am getting following exception while ftp file over to some other machine.

org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
    at org.apache.commons.net.io.Util.copyStream(Util.java:119)
    at org.apache.commons.net.io.Util.copyStream(Util.java:151)
    at org.apache.commons.net.io.Util.copyStream(Util.java:162)
    at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:373)
    at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1360)
    at com.fs.ftp.FTPUsingFTPClientApache.startFTP(FTPUsingFTPClientApache.java:40)
    at com.fs.ftp.FTPUsingFTPClientApache.main(FTPUsingFTPClientApache.java:17)

The Code that i am using for FTP is something like :-

FTPClient ftpClient = new FTPClient();
ftpClient.connect("home.abc.com");
ftpClient.login("remote", "guesst12");
int replyCode = ftpClient.getReplyCode();
if(FTPReply.isPositiveCompletion(replyCode)) {
    System.out.println("Connection proper");
}

if(ftpClient.changeWorkingDirectory("share")) {
    System.out.println("Directory Change Succesfull");
}
InputStream input = new FileInputStream(new File("H:/testFile.txt"));
BufferedInputStream inputStrean = new BufferedInputStream(input);
if(ftpClient.storeFile("testFile.txt", input)) {
    System.out.println("File Stored Successfully");
}
input.close();
inputStrean.close();
ftpClient.logout();
ftpClient.disconnect();

The above exception i get at line ftpClient.storeFile("testFile.txt", input).

Am i missing something, or using it not the correct way.

Thanks

回答1:

Catch that exception, call its getIOException() method to get the exception that caused the problem, an print its stacktrace. That will tell you what IOException caused the copy to fail.



回答2:

I want to support above solution, however I don't have enough reputation yet. That save me finally!

        ftps.setFileType(FTP.BINARY_FILE_TYPE);
        ftps.enterLocalPassiveMode();
        ftps.execPBSZ(0) ;
        ftps.execPROT("P") ;

By the way, my issue is "connection is reset during process of transfer". Below 2 commands are the key commands to me. ftps.execPBSZ(0) ; ftps.execPROT("P") ;



回答3:

If you have problems with file 0KB (example PDF files) once tranfered by ftps you have to force Passive Mode and set File Type

            ftps.setFileType(FTP.BINARY_FILE_TYPE);
            ftps.enterLocalPassiveMode();
            ftps.execPBSZ(0) ;
            ftps.execPROT("P") ;


回答4:

For my experience, this error was caused because the filesystem was full