简体   繁体   English

使用JSch从SFTP下载多个文件失败,并显示“请求队列:未知请求问题”

[英]Multiple file downloads from SFTP using JSch failing with “Request queue: Unknown request issue”

I am trying to download multiple files from FTP server. 我正在尝试从FTP服务器下载多个文件。 It works fine when i simply put it through a for loop . 当我只是将其放入for循环中时,它会很好地工作。 I don't know the file size which may vary , might be too small or too big. 我不知道文件大小可能会有所不同,可能太小或太大。 So, i am downloading them using executor service. 因此,我正在使用执行程序服务下载它们。 When i put the download function through executor, i get Request queue: Unknown request issue . 通过执行程序放置下载功能时,出现请求队列:未知请求问题。 Please let me know where i'm going wrong. 请让我知道我要去哪里错了。

System.out.println("Connecting to FTP Server");
Session session = null;
ChannelSftp channelSftp = null;

JSch jsch = new JSch();
session = jsch.getSession(sftpUser,sftpHost,22);        
session.setPassword(sftpPassword);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("Session Connected Status: "+session.isConnected());
Channel channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel; 
System.out.println("Channel Connected Status: "+channelSftp.isConnected());
System.out.println("File: "+sourceFilePath);

if(channelSftp.isConnected())
{
    String path = this.propertyManager.getValue("targetDirectoryPath");
    channelSftp.cd(path);
    Vector filelist = channelSftp.ls(path);

    for(int i=0; i<filelist.size();i++)
    {
        LsEntry entry = (LsEntry) filelist.get(i);
        String fileName     = sourceFilePath + entry.getFilename();
        destinationFilePath =  ShipmentUtils.getDownloadPath(entry.getFilename());

        System.err.println(entry.getFilename());
        exec.execute(new FileDownloader(destinationFilePath, channelSftp.get(fileName)));
    }
}
channelSftp.disconnect();
session.disconnect();

Here is the downloader class 这是下载程序类

private class FileDownloader implements Runnable
{
    String destinationFilePath;
    InputStream stream;

    public  FileDownloader(String destinationFilePath , InputStream stream) {
        this.destinationFilePath = destinationFilePath;
        this.stream = stream;
    }

    @Override
    public void run() {
        byte[] buffer = new byte[1024];
        BufferedOutputStream bos = null;
        BufferedInputStream   bis = null ;

        try
        {
            bis = new BufferedInputStream(stream);
            File newFile = new File(destinationFilePath);
            OutputStream os = new FileOutputStream(newFile);

            bos = new BufferedOutputStream(os);
            int readCount=0;
            while( (readCount = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, readCount);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(bis != null) bis.close();
                if(bos != null) bos.close();    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

This is the error 这是错误

java.io.IOException: error: 4: RequestQueue: unknown request id 1399811183
    at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1406)
    at java.io.BufferedInputStream.fill(Unknown Source)
    at java.io.BufferedInputStream.read1(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at java.io.FilterInputStream.read(Unknown Source)
    at com.shipmentprocessing.network.FTPConnector$FileDownloader.run(FTPConnector.java:141)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
java.io.IOException: error
    at com.jcraft.jsch.ChannelSftp$2.close(ChannelSftp.java:1505)
    at java.io.BufferedInputStream.close(Unknown Source)
    at com.shipmentprocessing.network.FTPConnector$FileDownloader.run(FTPConnector.java:150)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

While you can use one SFTP connection/JSch session for multiple downloads, the JSch session is definitely not thread safe. 虽然您可以使用一个SFTP连接/ JSch会话进行多次下载,但JSch会话绝对不是线程安全的。

So if you need to run the downloads is separate threads, you have to open new connection for (in) each thread. 因此,如果您需要运行单独的线程进行下载,则必须为每个线程(在其中)打开新的连接。 Your download performance will be much better this way too. 这样,您的下载性能也会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM