2

I am new to Grails framework. I have a requirement to upload text file to remote FTP Server through batch application in Grails. Request if anyone has a suggestion, step by step process to how to connect to remote server and upload the file.

Thanks in advance.

madhavi
  • 33
  • 6

1 Answers1

8

First, include commons-net in your BuildConfig.groovy (Grails 2.x) or build.gradle (Grails 3.x):

compile 'commons-net:commons-net:3.3'

Secondly add a service:

class UploadService {
    GrailsApplication grailsApplication

    String upload(String fileName, InputStream inputStream) {
        String status
        new FTPClient().with {
            connect grailsApplication.config.getProperty('ftp.host')
            login grailsApplication.config.getProperty('ftp.username'), grailsApplication.config.getProperty('ftp.password')
            enterLocalPassiveMode()
            setFileType(BINARY_FILE_TYPE)
            changeWorkingDirectory grailsApplication.config.getProperty('ftp.uploadDir')
            storeFile(fileName, inputStream)
            status = replyString
            disconnect()
        }
        return status
    }
}

Update:

I forgot to add the configuration that needs to go into grails-app/conf/Config.groovy (Grails 2.x) or grails-app/config/application.groovy:

ftp {
    host = 'some.host'
    username = 'myuser'
    password = '*****'
    uploadDir = 'Uploads'
}

It would also be possible to create the FTPClient() instance in resources.groovy and use dependency injection instead.

bean = {
    ftpClient(FTPClient)
}

and then have this in the service:

class UploadService {
     GrailsApplication grailsApplication
     FTPClient ftpClient // Will be injected

     String upload(String fileName, InputStream inputStream) {

         String status
         ftpClient.with {
              // Same as above
         }
     }
}

Which would allow you to unit-test the class, where you simply mock out FTPClient

sbglasius
  • 3,104
  • 20
  • 28
  • Hello. Thank you very much for the quick help. I did a quick test it works. – madhavi Nov 08 '15 at 22:22
  • when i am invoking this service from another service,getting exception : ftp.UploadService.connect() is applicable for argument types: (groovy.util.ConfigObject) values: [[:]], and in unit test class 'grailsApplication' is null – madhavi Nov 09 '15 at 06:11
  • I was able to test the connection by hard coded values for user name ,password....but when i use grailsApplication variable ,getting above exception. Is it possible to declare FTPClient in resources.groovy ?so that i can read the properties from property file,if yes can you pls .suggest me how can i declare it. – madhavi Nov 09 '15 at 06:29
  • Exception:Caused by MissingMethodException: No signature of method: com.ftp.UploadService.connect() is applicable for argument types: (groovy.util.ConfigObject) values: [[:]] – madhavi Nov 09 '15 at 06:34
  • Your issues are not due to @sbglasius solution. You are trying to get a value from your configuration, that does not exist, and pass it to the service. I suggest you accept the answer and create a new question. For your issue to access your values in Config.groovy – Luis Muñiz Nov 10 '15 at 04:56
  • @madhavi Please accept the answer as the correct one, thank you. – sbglasius Sep 22 '16 at 07:23