从FTP服务器下载文件时出现groovy.lang.MissingPropertyException

时间:2015-08-24 13:47:24

标签: groovy ftp

  

我想在grails中创建从ftp下载文件的作业   服务器在一定的时间间隔后说2-3天并将其存储起来   指定的本地路径。写入相同的代码并进行微小更改   java工作正常,但在Grails中编写类似的代码时   我正面临错误而无法解决它。可以任何身体告诉我   我犯错的地方?

以下是我在工作开始时遇到的错误。

JOB STARTED::************************************************************************************
2015-08-24 18:20:35,285 INFO    org.quartz.core.JobRunShell:207 Job GRAILS_JOBS.com.hoteligence.connector.job.DownloadIpgGzipFilesJob threw a JobExecutionException: 
org.quartz.JobExecutionException: groovy.lang.MissingPropertyException: No such property: ftpClient for class: com.hoteligence.connector.job.DownloadIpgGzipFilesJob [See nested exception: groovy.lang.MissingPropertyException: No such property: ftpClient for class: com.hoteligence.connector.job.DownloadIpgGzipFilesJob]
    at grails.plugins.quartz.GrailsJobFactory$GrailsJob.execute(GrailsJobFactory.java:111)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: groovy.lang.MissingPropertyException: No such property: ftpClient for class: com.hoteligence.connector.job.DownloadIpgGzipFilesJob
    at com.hoteligence.connector.job.DownloadIpgGzipFilesJob.execute(DownloadIpgGzipFilesJob.groovy:93)
    at grails.plugins.quartz.GrailsJobFactory$GrailsJob.execute(GrailsJobFactory.java:104)
    ... 2 more
            /* I've added all the related dependencies in grails Build Config.
            */

package com.hoteligence.connector.job

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.codehaus.groovy.grails.commons.ConfigurationHolder as ConfigHolder;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
 * @author Gajanan
 * this is back-end job which download Files from ftp server and store it on locally
 */
class DownloadIpgGzipFilesJob {


    static triggers = {
        simple repeatInterval: Long.parseLong(ConfigHolder.config.DEVICE_PING_ALERT_JOB_REPEAT_INTERVAL),
        startDelay : 60000
    }

    def execute() {

        try{

            println "JOB STARTED::************************************************************************************";
            /* following is the details which are required for server connectivity  
            */
            String server = ConfigHolder.config.IPG_SERVER_NAME;
            int port = ConfigHolder.config.IPG_SERVER_PORT_NO;
            String user = ConfigHolder.config.IPG_SERVER_USER_NAME;
            String pass = ConfigHolder.config.IPG_SERVER_USER_PASSWORD;
            String [] fileNames = ConfigHolder.config.IPG_DOWNLOADABLE_GZIP_FILE_LIST.split(",");
            String downloadFilePath = ConfigHolder.config.IPG_GZIP_DOWNLOAD_LOCATION;
            String fileDate = (todaysDate.getYear()+1900)+""+((todaysDate.getMonth()+1)<=9?("0"+(todaysDate.getMonth()+1)):(todaysDate.getMonth()+1))+""+todaysDate.getDate();

            FTPClient ftpClient = new FTPClient();


            /* Here we are making connection to the server and the reply
             from server is printed on console
            */

            ftpClient.connect(server, port);
            showServerReply(ftpClient);

            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                System.out.println("Connect failed");
                return;
            }

            boolean success = ftpClient.login(user, pass);
            showServerReply(ftpClient);

            if (!success) {
                System.out.println("Could not login to the server");
                return;
            }



            /* Here we are iterate the FileList and download them to specified directory

            */
            for(int i =0; i<fileNames.length;i++) {
                String fileName = "on_"+ConfigHolder.config.IPG_DATA_COUNTRY_CODE+fileNames[i]+fileDate+".xml.gz";

                System.out.println(fileName);
                downloadFtpFileByName(ftpClient,fileName,downloadFilePath+fileName);
            }
        }
        catch (IOException ex) {
            System.out.println("Oops! Something wrong happened");
            ex.printStackTrace();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            // logs out and disconnects from server

            /* In finally block we forcefully close the connection and close the file node also

            */
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

            /* this function is nothing but to print the ftp server reply after connection to ftp server

            */
    private static void showServerReply(FTPClient ftpClient) {
        String[] replies = ftpClient.getReplyStrings();
        if (replies != null && replies.length > 0) {
            for (String aReply : replies) {
                System.out.println("SERVER: " + aReply);
            }
        }
    }


            /* This is the actual logic where we copy the file from ftp 
             and store on local directory
             this method accept three parameter FtpClient object, Name of the file which has to be downloaded from server and the path where downloaded file has to be stored
            */

    private static void downloadFtpFileByName(FTPClient ftpClient,String fileName,String downloadfileName){

        System.out.println("Strat Time::"+System.currentTimeMillis());
        try {

            String remoteFile1 = "/"+fileName; // file on server 
            File downloadFile1 = new File(downloadfileName); // new file which is going to be copied on local directory
            OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1)); 
            Boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);


            if (success) {
                System.out.println("File"+fileName+" has been downloaded successfully.");
            }
            else
            {
                System.out.println("File"+fileName+" has been DOWNLOAD FAILURE....");
            }

            outputStream1.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("END Time::"+System.currentTimeMillis());
    }

}

1 个答案:

答案 0 :(得分:0)

移动此行:

        FTPClient ftpClient = new FTPClient();

try { ... } catch()区块之外(即,将其移至try之前)

您在try中声明了局部变量,然后尝试在finally块中使用它

相关问题