1

我创建了一个使用 Apache Commons VFS2 和 JSCH 库将文件上传到 SFTP 服务器的类。第一次(通常)19次转移一切正常,但在那之后我开始出现异常

SEVERE: org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at "sftp://username:***@localhost/".
at org.apache.commons.vfs2.provider.sftp.SftpFileProvider.doCreateFileSystem(SftpFileProvider.java:107)
at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.getFileSystem(AbstractOriginatingFileProvider.java:103)
at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:81)
at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:65)
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:693)
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:621)
at com.mycompany.ftp.FtpTransfer.upload(FtpTransfer.java:74)
at com.mycompany.ftp.FtpTransfer.upload(FtpTransfer.java:62)
at com.mycompany.ftp.FtpTransfer.upload(FtpTransfer.java:55)
at com.mycompany.ftp.FtpTest.testMultipleUploads(FtpTest.java:183)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at "localhost".
at org.apache.commons.vfs2.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:230)
at org.apache.commons.vfs2.provider.sftp.SftpFileProvider.doCreateFileSystem(SftpFileProvider.java:96)
... 32 more
Caused by: com.jcraft.jsch.JSchException: Session.connect: java.net.SocketException: Connection reset
at com.jcraft.jsch.Session.connect(Session.java:558)
at com.jcraft.jsch.Session.connect(Session.java:183)
at org.apache.commons.vfs2.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:226)

现在,如果在每次传输后休眠让我们说三秒钟,我根本不会遇到异常。

这是代码:

public void upload() throws Exception {         
    StandardFileSystemManager manager = new StandardFileSystemManager();
    FileObject localFile = null;
    FileObject remoteFile = null;
    try {                       
        // Initializes the file manager
        manager.init();
        // Setup our SFTP configuration
        FileSystemOptions opts = new FileSystemOptions();
        SftpFileSystemConfigBuilder configBuilder = SftpFileSystemConfigBuilder.getInstance();

        configBuilder.setStrictHostKeyChecking(opts, "no");
        configBuilder.setUserDirIsRoot(opts, true);
        configBuilder.setTimeout(opts, 10000);

        // Create local file object
        File file = new File(fileName);     
        localFile = manager.resolveFile(file.getAbsolutePath());

        // Create remote file object
        remoteFile = manager.resolveFile(this.createConnectionString().toString(),opts);

        // Copy local file to sftp server
        remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);      

    } catch (Exception e) {
        writeExceptionLog(e);
        throw e;
    } finally {         
        try {
            if(localFile != null)
                localFile.close();
            if(remoteFile != null) 
                remoteFile.close();
            manager.close();            
        } catch (FileSystemException e) {
            writeExceptionLog(e);
            throw e;
        }           
    }

是否有一些资源需要释放,或者只是需要时间在内部释放它们?

4

0 回答 0