java 使用 FTPClient FTP 文件时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6717684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting an error while FTP file using FTPClient
提问by M.J.
I am getting following exception while ftp file over to some other machine.
当 ftp 文件转移到其他机器时,我收到以下异常。
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 :-
我用于 FTP 的代码类似于:-
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)
.
上面的异常我在线上得到ftpClient.storeFile("testFile.txt", input)
。
Am i missing something, or using it not the correct way.
我是不是遗漏了什么,或者使用方法不正确。
Thanks
谢谢
采纳答案by Stephen C
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.
捕获该异常,调用其getIOException()
方法以获取导致问题的异常,并打印其堆栈跟踪。这将告诉您IOException
导致复制失败的原因。
回答by Fabius Chiang
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") ;
顺便说一下,我的问题是“在传输过程中连接被重置”。下面 2 个命令是我的关键命令。ftps.execPBSZ(0) ; ftps.execPROT("P") ;
回答by Genaut
For my experience, this error was caused because the filesystem was full
根据我的经验,这个错误是因为文件系统已满
回答by Rod SolPar
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 传输后遇到文件 0KB(示例 PDF 文件)的问题,您必须强制使用被动模式并设置文件类型
ftps.setFileType(FTP.BINARY_FILE_TYPE);
ftps.enterLocalPassiveMode();
ftps.execPBSZ(0) ;
ftps.execPROT("P") ;