Getting an error while FTP file using FTPClient

人走茶凉 提交于 2019-12-11 03:46:27

问题


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:


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




回答4:


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") ;


来源:https://stackoverflow.com/questions/6717684/getting-an-error-while-ftp-file-using-ftpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!