本文介绍了如何在grails中将文件上传到远程FTP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Grails框架的新手。我需要通过Grails中的批量应用程序将文本文件上传到远程FTP服务器。请求如果有人有建议,一步一步的过程如何连接到远程服务器并上传文件。



预先致谢。


首先,在你的BuildConfig.groovy(Grails 2.x)或build.gradle(Grails 3.x)中包含commons-net:

b
$ b

compile'c​​ommons-net:commons-net:3.3' $ b

其次添加一个服务:

$ p $ class UploadService {
GrailsApplication grailsApplication

String upload(String fileName,InputStream inputStream){
字符串状态
新FTPClient()。带{
连接grailsApplication.config.getProperty('ftp.host')
登录grailsApplication.config.getProperty ('ftp.username'),grailsApplication.config.getProperty('ftp.password')
enterLocalPassiveMode()
setFileType(BINARY_FILE_TYPE)
changeWorkingDirectory grailsApplic ation.config.getProperty('ftp.uploadDir')
storeFile(fileName,inputStream)
status = replyString
disconnect()
}
返回状态


$ / code $

$ b

更新:

我忘了添加需要进入 grails-app / conf / Config.groovy (Grails 2.x)或 grails的配置-app / config / application.groovy

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

也可以在 resources.groovy 中创建FTPClient()实例并使用依赖关系而不是注射。

  bean = {
ftpClient(FTPClient)
}

然后在服务中使用:

  class UploadService {
GrailsAppli阳离子grailsApplication
FTPClient ftpClient //将被注入

字符串上传(String fileName,InputStream inputStream){

字符串状态
ftpClient.with {
//同上
}
}
}

这将允许你对这个类进行单元测试,你只需简单地模拟出 FTPClient


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.

解决方案

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

这篇关于如何在grails中将文件上传到远程FTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 11:50