【问题标题】:Java FTP file get issueJava FTP文件获取问题
【发布时间】:2011-09-27 12:26:49
【问题描述】:

我有一个按计划运行的应用程序。它连接到 ftp 服务器并从远程文件夹获取文件。计划程序每 5 分钟运行一次。有时当远程位置有很多文件时,计划程序会在第一个周期时再次运行运行。在这种情况下,有时它会下载 0 大小的文件,即使远程位置的实际文件大小大于 0。有人知道为什么会发生这种情况吗?

下面是导入文件的代码。

private void importEDIFiles(String host, String user, String password, String path, String road) {
        try {
            String edi824Path = path + "/" + EDI_824_FOLDER;
            FTPBroker ftpBroker = new FTPBroker(host, user, password, edi824Path);

            FTPClient client = ftpBroker.makeFTPConeection();
            String os = client.getSystemName();
            client.setFileTransferMode(FTP.ASCII_FILE_TYPE);
            File edi824File = null;
            File edi824Filebak = null;
            ArrayList<FTPFile> files;
            try {

                FTPFile[] ftpfiles = client.listFiles();

                logger.info("\t" + ftpfiles.length + " files are in ftp location ");
                if (ftpfiles.length > 0) {
                    files = removeZeroFiles(ftpfiles);
                     for(int x=0;x<files.size();x++){
                         logger.info("name  ---"+files.get(x).getName());
                         logger.info("size ----"+files.get(x).getSize());
                     }
                    String ftpFile = null;
                    logger.info("\t" + files.size() + " downloading from " + road + " rail road.");
                    for (int i = 0; i < files.size(); i++) {
                        ftpFile = files.get(i).getName();
                        logger.info("\t" + ftpFile + " is downloading....");
//                        logger.info("\t" + ftpFile + " size ...." + ftpFile.isEmpty());

                        String source = destinationFilePath + pathSeparator + road + pathSeparator + ftpFile;
                        String target = edi_824backupFilePath + pathSeparator + road + pathSeparator + ftpFile;
                        edi824File = new File(source);
                        edi824Filebak = new File(target);
                        FileOutputStream fosout = new FileOutputStream(source);

                        boolean isRetrieved = client.retrieveFile(ftpFile, fosout);
                        logger.debug("isRetrieved : " + isRetrieved);
                        FileUtils.copyFile(edi824File,edi824Filebak);
                        fosout.flush();
                        fosout.close();


                        boolean isDelete = client.deleteFile(ftpFile);
                        logger.debug("isDelete : " + isDelete);

                    }

                } else {
                    logger.info("No files to Pull in the FTP Location for " + user);
                    //throw new RuntimeException("No files to Pull in  FTP Location.");
                }
            } catch (Exception e) {
                logger.error(e,e);
                e.printStackTrace();
            } finally {
                client.logout();
                client.disconnect();


            }


        } catch (Exception ex) {
            logger.error(ex, ex);
            ex.printStackTrace();
        }


    }

【问题讨论】:

    标签: java spring ftp


    【解决方案1】:

    您可以使用标志 boolean isRunning()、setRunning(boolean) 并同步您的代码,这样两个或多个线程就不会同时运行同一个方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多