即使从 ftp 服务器下载其文件并且不重复使用自身来下载另一个文件,线程也不会与服务器断开连接

发布于 2025-01-10 16:30:49 字数 3400 浏览 0 评论 0原文

这是一个代码片段。

FtpDownloader.java

ExecutorService pool = Executors.newFixedThreadPool(5);

 for (FTPFile file : files) {
     if (!file.isFile()) continue;
     pool.submit(new FtpFileDownloader(file));
}

FTPFileDownloader.java


public class FtpFileDownloader implements Runnable{
    static Logger logger = LoggerFactory.getLogger(FtpFileDownloader.class);

    private FTPFile file;

    public FtpFileDownloader(FTPFile file) {
        this.file = file;
    }

    private OutputStream outputStream;

    @Override
    public void run() {
        try{
            long start = System.currentTimeMillis();
            String fileName = file.getName();
            logger.info("File is {}", fileName);

            outputStream = new BufferedOutputStream(new FileOutputStream("/home/user/Downloads/" + "FtpDownloads" + "/" + fileName));

            //get the file from the remote system
            ftpClient.retrieveFile(fileName, outputStream);

            showServerReply(ftpClient);
            logger.info("[{}ms, {} processing finished.]",System.currentTimeMillis()-start,fileName);


        }catch (Exception e){
            logger.info("FtpFileDownloader expection");
            e.printStackTrace();
        }finally {
            try {
                //close output stream
                outputStream.close();
            } catch (IOException e) {
                logger.info("Io exception happened");
                e.printStackTrace();
            }
        }

我创建了一个大小为 5 的固定线程池。 因此,在每个单独的服务器从服务器下载 5 个文件后,

即使下载了文件并等待 FTP 服务器在下载文件后断开连接,线程也不会与服务器断开连接,

2022-03-01 15:49:33.584  INFO 10931 --- [pool-1-thread-5] t.a.f.listener.ftp.FtpFileDownloader     : File is mail-send-winforms.png
2022-03-01 15:49:33.587  INFO 10931 --- [pool-1-thread-4] t.a.f.listener.ftp.FtpFileDownloader     : File is mail-editor.png
2022-03-01 15:50:33.769  INFO 10931 --- [pool-1-thread-1] t.a.f.listener.ftp.FtpFileDownloader     : FtpFileDownloader expection
2022-03-01 15:50:33.771  INFO 10931 --- [pool-1-thread-1] t.a.f.listener.ftp.FtpFileDownloader     : File is mime-explorer.png
java.net.SocketTimeoutException: Connect timed out
    at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:546)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:633)
    at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:866)
    at org.apache.commons.net.ftp.FTPClient._retrieveFile(FTPClient.java:971)
    at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:3308)
    at tech.adoptnet.ftppractice.listener.ftp.FtpFileDownloader.run(FtpFileDownloader.java:35)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:833)

每个线程都在等待,直到连接超时,然后转到下载其他文件。

如何使线程在没有连接超时的情况下重复使用?

This is a code snippet.

FtpDownloader.java

ExecutorService pool = Executors.newFixedThreadPool(5);

 for (FTPFile file : files) {
     if (!file.isFile()) continue;
     pool.submit(new FtpFileDownloader(file));
}

FTPFileDownloader.java


public class FtpFileDownloader implements Runnable{
    static Logger logger = LoggerFactory.getLogger(FtpFileDownloader.class);

    private FTPFile file;

    public FtpFileDownloader(FTPFile file) {
        this.file = file;
    }

    private OutputStream outputStream;

    @Override
    public void run() {
        try{
            long start = System.currentTimeMillis();
            String fileName = file.getName();
            logger.info("File is {}", fileName);

            outputStream = new BufferedOutputStream(new FileOutputStream("/home/user/Downloads/" + "FtpDownloads" + "/" + fileName));

            //get the file from the remote system
            ftpClient.retrieveFile(fileName, outputStream);

            showServerReply(ftpClient);
            logger.info("[{}ms, {} processing finished.]",System.currentTimeMillis()-start,fileName);


        }catch (Exception e){
            logger.info("FtpFileDownloader expection");
            e.printStackTrace();
        }finally {
            try {
                //close output stream
                outputStream.close();
            } catch (IOException e) {
                logger.info("Io exception happened");
                e.printStackTrace();
            }
        }

I've created a fixed thread pool of size 5.
So after downloading 5 files from the server by each individual server

Thread is not disconnecting from the server even after its file is downloaded and waiting there for FTP server to disconnect

2022-03-01 15:49:33.584  INFO 10931 --- [pool-1-thread-5] t.a.f.listener.ftp.FtpFileDownloader     : File is mail-send-winforms.png
2022-03-01 15:49:33.587  INFO 10931 --- [pool-1-thread-4] t.a.f.listener.ftp.FtpFileDownloader     : File is mail-editor.png
2022-03-01 15:50:33.769  INFO 10931 --- [pool-1-thread-1] t.a.f.listener.ftp.FtpFileDownloader     : FtpFileDownloader expection
2022-03-01 15:50:33.771  INFO 10931 --- [pool-1-thread-1] t.a.f.listener.ftp.FtpFileDownloader     : File is mime-explorer.png
java.net.SocketTimeoutException: Connect timed out
    at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:546)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:633)
    at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:866)
    at org.apache.commons.net.ftp.FTPClient._retrieveFile(FTPClient.java:971)
    at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:3308)
    at tech.adoptnet.ftppractice.listener.ftp.FtpFileDownloader.run(FtpFileDownloader.java:35)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:833)

after downloading the files every thread is waiting until connection time out and then it goes to download other files.

How to make thread resuse itself without connection time out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情栀口红 2025-01-17 16:30:49

我想我明白你想做的事。首先,FTP 本身不允许您同时使用多个连接(参考 与 Java FTP 客户端并行上传文件)。

你所做的也不是线程安全的。所以我建议你建立并行连接并从那里下载。

I think I got what you're trying to do. First of all, FTP doesn't itself let you use multiple connections simultaneously (For ref Uploading files in parallel with Java FTP client).

What you're doing is not thread-safe as well. So I advise you to make parallel connections and download from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文