
本文介绍了如何使用 Java JSch 库在 SFTP 服务器上解压 ZIP 文件。通过创建独立的 SFTP 通道分别用于读取和写入操作,解决了 JSch 库在单个通道上同时处理多个文件流时可能出现的问题。同时,文章也提醒读者注意代码的实际行为,即先下载 ZIP 文件到本地解压,再将解压后的文件上传到 SFTP 服务器。
在使用 JSch 库处理 SFTP 服务器上的 ZIP 文件时,可能会遇到 java.io.IOException: error: 4: RequestQueue: unknown request id 错误。这通常是由于 JSch 库在单个 SFTP 通道上同时处理多个文件流时出现的问题。虽然理论上可以在一个 SFTP 通道上打开多个文件,但 JSch 库似乎对此支持不够完善。
解决方法:使用独立的 SFTP 通道
为了解决这个问题,可以为读取(get)操作和写入(put)操作分别创建独立的 SFTP 通道。 这样可以避免 JSch 库在单个通道上同时处理多个文件流时可能出现的冲突。
立即学习“Java免费学习笔记(深入)”;
以下是修改后的代码示例:
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class SftpUnzip {
    public static void unzipSftpFile(String host, int port, String username, String password, String zipFilePath) throws Exception {
        JSch jsch = new JSch();
        Session session = jsch.getSession(username, host, port);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no"); // 忽略主机密钥检查,生产环境不建议这样做
        session.connect();
        ChannelSftp getChannel = (ChannelSftp) session.openChannel("sftp");
        getChannel.connect();
        ChannelSftp putChannel = (ChannelSftp) session.openChannel("sftp");
        putChannel.connect();
        try {
            // 获取ZIP文件列表
            Vector<LsEntry> filelist = getChannel.ls(zipFilePath);
            for (LsEntry entry : filelist) {
                if (entry.getFilename().endsWith(".zip")) { // 确保只处理ZIP文件
                    try (
                            InputStream inputStream = getChannel.get(entry.getFilename());
                            ZipInputStream zipInputStream = new ZipInputStream(inputStream)
                    ) {
                        ZipEntry zipEntry;
                        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                            if (!zipEntry.isDirectory()) { // 忽略目录
                                String entryName = zipEntry.getName();
                                try (OutputStream outputStream = putChannel.put(entryName)) {
                                    byte[] buffer = new byte[2048];
                                    int len;
                                    while ((len = zipInputStream.read(buffer)) != -1) {
                                        outputStream.write(buffer, 0, len);
                                    }
                                } catch (SftpException e) {
                                    System.err.println("Error writing file: " + entryName + " - " + e.getMessage());
                                }
                            }
                            zipInputStream.closeEntry();
                        }
                    } catch (IOException e) {
                        System.err.println("Error processing ZIP entry: " + e.getMessage());
                    }
                }
            }
        } catch (SftpException e) {
            System.err.println("Error listing or getting file: " + e.getMessage());
        } finally {
            if (getChannel != null && getChannel.isConnected()) {
                getChannel.disconnect();
            }
            if (putChannel != null && putChannel.isConnected()) {
                putChannel.disconnect();
            }
            if (session != null && session.isConnected()) {
                session.disconnect();
            }
        }
    }
    public static void main(String[] args) {
        String host = "your_sftp_host";
        int port = 22; // 默认 SFTP 端口
        String username = "your_username";
        String password = "your_password";
        String zipFilePath = "path/to/your/zip/files"; // ZIP 文件所在的路径
        try {
            unzipSftpFile(host, port, username, password, zipFilePath);
            System.out.println("Unzip process completed.");
        } catch (Exception e) {
            System.err.println("An error occurred: " + e.getMessage());
            e.printStackTrace();
        }
    }
}代码解释:
注意事项:
总结:
虽然上述代码可以解决 JSch 库在单个通道上同时处理多个文件流时可能出现的问题,但需要注意的是,它实际上是将 ZIP 文件下载到本地解压,然后再将解压后的文件上传到 SFTP 服务器。如果 ZIP 文件非常大,这可能会消耗大量的本地资源和网络带宽。
如果需要直接在 SFTP 服务器上解压 ZIP 文件,可能需要考虑使用其他工具或方法,例如:
选择哪种方法取决于具体的应用场景和需求。
以上就是使用 Java JSch 在 SFTP 服务器上解压文件的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号