我正在尝试使用apache-commons的FTPClient类将文件上传到 FTP 服务器。storeFile () 没有错误,但我不想使用它,而是想创建一个侦听器,以便我可以关注上传进度。
当我运行代码时,它会愉快地连接并调用uploadFile () - 这又会引发CopyStreamException: IOException。
我不擅长决定引发异常的原因,它可能是我的“监听器”对象吗?
我就是这样做的:
/** Upload a file to the server */
public boolean uploadFile (String localFile, String serverFile, CopyStreamAdapter listener ) throws IOException, FTPConnectionClosedException
{
/*
FileInputStream in = new FileInputStream(localFile);
boolean result = storeFile(serverFile, in);
in.close();
return result;
*/
InputStream stO = new BufferedInputStream( this.retrieveFileStream(serverFile), this.getBufferSize());
OutputStream stD = new FileOutputStream(localFile);
org.apache.commons.net.io.Util.copyStream(stO, stD, this.getBufferSize(), org.apache.commons.net.io.CopyStreamEvent.UNKNOWN_STREAM_SIZE, listener );
this.completePendingCommand();
// if we get this far, it's rainbows all the way down!
return true ;
}
听众:
CopyStreamAdapter listener = new org.apache.commons.net.io.CopyStreamAdapter()
{
public void bytesTransferred (long totalBytesTransferred, int bytesTransferred, long streamSize)
{
// show progress
System.out.println( "Upload progress: " + bytesTransferred + " of " + totalBytesTransferred ) ;
}
} ;
f = new ftpClientWrapper() ; // my class instance.
f.uploadFile(filename, "/public/dropfolder/test/testfile.test" , listener ) ; // i've validated filename and uploadpath
编辑:这是堆栈跟踪:
Error on FTP upload: org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
at org.apache.commons.net.io.Util.copyStream(Util.java:129)
at org.apache.commons.net.io.Util.copyStream(Util.java:173)
at dk.capsize.ftpapp.ftpClientWrapper.uploadFile(JakartaFtpWrapper.java:105)
at FTPApp.main(FTPApp.java:63)
提前致谢!