Apache-Commons:使用 CopyStreamEvent 时出现 CopyStreamException:IOException

发布于 2024-10-22 05:38:23 字数 2113 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文