Apache-Commons:使用 CopyStreamEvent 时出现 CopyStreamException:IOException
我正在尝试使用 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)
提前致谢!
I am trying to upload a file to an FTP server using the apache-commons' FTPClient class. storeFile() works with no error, but instead of using it, I wanted to create a listener so I could keep an eye on the upload progress.
When I run the code it happily connects and calls uploadFile() - Which in turn throws the CopyStreamException: IOException.
I'm not good at deciding what causes the exception to be thrown, could it be my "listener" object?
This is how I do it:
/** 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 ;
}
listener:
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
edit: This is the stack trace:
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)
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论