FiblackBrother opened a new issue, #854:
URL: https://github.com/apache/mina-sshd/issues/854

   In a 10 gigabit network environment, there is 1MB+per second. I tried using 
multi-threaded block reading, with 4 threads reading 5MB per second. I feel 
this is not quite right. My test code example is as follows
   `
   String username = args[0];
           String password = args[1];
           String host = args[2];
           int port = Integer.parseInt(args[3]);
           String fileName = args[4];
           long filesize = Long.parseLong(args[5]);
   
           SshClient sshClient = null;
           try {
               sshClient = SshClient.setUpDefaultClient();
               sshClient.start(); // 启动SshClient,必须在connect之前调用
               sshClient.setServerKeyVerifier((clientSession, socketAddress, 
publicKey) -> true);
               // 使用配置的连接超时时间(基于listTimeout)
               // 必须先调用verify()等待连接完成,然后再获取session
               try (ClientSession clientSession = sshClient.connect(username, 
host, port)
                       .verify(60, TimeUnit.SECONDS)
                       .getSession()) {
                   // 检查会话是否已关闭
                   if (!clientSession.isOpen()) {
                       throw new IOException("SSH会话在连接后立即关闭");
                   }
                   clientSession.addPasswordIdentity(password);
   
                   // 使用配置的认证超时时间(基于listTimeout)
                   org.apache.sshd.client.future.AuthFuture authFuture = 
clientSession.auth();
                   if (!authFuture.verify(60, TimeUnit.SECONDS).isSuccess()) {
                       throw new IOException("SSH认证失败");
                   }
   
                   // 认证成功后,再次检查会话状态
                   if (!clientSession.isOpen() || 
!clientSession.isAuthenticated()) {
                       throw new IOException("SSH会话在认证后状态异常");
                   }
   
                   System.out.println("成功创建SSH会话.");
                   // 创建SFTP客户端(带超时保护)
                   SftpClient sftpClient = 
SftpClientFactory.instance().createSftpClient(clientSession);
                   long t1 = System.currentTimeMillis();
                   //分块下载
   //                new test().ultraDownload(clientSession, sftpClient, 
fileName, fileName);
                   // 打开文件获取客户端文件句柄
                   SftpClient.CloseableHandle fileHandle = 
sftpClient.open(fileName, EnumSet.of(SftpClient.OpenMode.Read));
   
                   try (SftpClient.CloseableHandle handle = 
sftpClient.open(fileName, SftpClient.OpenMode.Read)) {
   
                       // 正确的方式:使用带有缓冲区的读取
                       byte[] buffer = new byte[10*1024 * 1024]; // 64KB 缓冲区
                       long fileOffset = 0L;
                       long totalBytes = 0;
   
                       while (true) {
                           int bytesRead = sftpClient.read(handle, fileOffset, 
buffer, 0, buffer.length);
                           if (bytesRead == -1) {
                               break; // 文件结束
                           }
                           if (bytesRead > 0) {
                               totalBytes += bytesRead;
                               fileOffset += bytesRead;
   
                               // 进度监控
                               if (totalBytes % (5 * 1024 * 1024) == 0) {
                                   System.out.printf("下载进度: %d MB%n", 
totalBytes / (1024 * 1024));
                               }
                           }
                       }
   
                       System.out.printf("文件下载完成,大小: %d MB%n", totalBytes / 
(1024 * 1024));
   
                   } catch (IOException e) {
                       e.printStackTrace();
                   }
                   long t2 = System.currentTimeMillis();
                   System.out.println("sftpClient.read耗时:"+(t2-t1));
   
                   sftpClient.close();
               }
           } catch (Exception e) {
               e.printStackTrace();
           } finally {
               // 确保SshClient正确关闭
               if (sshClient != null) {
                   try {
                       sshClient.stop();
                       sshClient.close();
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               }
           }
   `


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to