问题描述
我是Grails框架的新手。我需要通过Grails中的批量应用程序将文本文件上传到远程FTP服务器。请求如果有人有建议,一步一步的过程如何连接到远程服务器并上传文件。
预先致谢。
首先,在你的BuildConfig.groovy(Grails 2.x)或build.gradle(Grails 3.x)中包含commons-net:
我是Grails框架的新手。我需要通过Grails中的批量应用程序将文本文件上传到远程FTP服务器。请求如果有人有建议,一步一步的过程如何连接到远程服务器并上传文件。
预先致谢。
b
$ b
其次添加一个服务: 更新: 我忘了添加需要进入 也可以在 然后在服务中使用: 这将允许你对这个类进行单元测试,你只需简单地模拟出 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): Secondly add a service: Update: I forgot to add the configuration that needs to go into It would also be possible to create the FTPClient() instance in and then have this in the service: Which would allow you to unit-test the class, where you simply mock out 这篇关于如何在grails中将文件上传到远程FTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! compile'commons-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
compile 'commons-net:commons-net:3.3'
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
}
}
grails-app/conf/Config.groovy
(Grails 2.x) or grails-app/config/application.groovy
:ftp {
host = 'some.host'
username = 'myuser'
password = '*****'
uploadDir = 'Uploads'
}
resources.groovy
and use dependency injection instead.bean = {
ftpClient(FTPClient)
}
class UploadService {
GrailsApplication grailsApplication
FTPClient ftpClient // Will be injected
String upload(String fileName, InputStream inputStream) {
String status
ftpClient.with {
// Same as above
}
}
}
FTPClient