簡體   English   中英

從FTP服務器下載文件時出現groovy.lang.MissingPropertyException

[英]groovy.lang.MissingPropertyException while Downloading Files from FTP Server

我想在grails中創建作業,該作業在一定時間間隔(例如2-3天)后從ftp服務器下載文件,並將其存儲在指定的本地路徑上。 相同的代碼進行了細微的更改,用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());
    }

}

移動這一行:

        FTPClient ftpClient = new FTPClient();

try { ... } catch()塊之外(即,將其移至try之前)

您在try中聲明局部變量,然后嘗試在finally塊中使用它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM