I have a grails app which should upload a bunch of images to a server via FTP. To do that I`m using commons-net. What is weird is, that if I create a new connection for each file it works normal, but if I have connected once and then start sending the files, the files got corrupted! bellow is my code, which works but I dont want to create a new connection for each file:
filesList.each{ f->
String ftpUser = ConfigurationHolder.config.ftp.user
String ftpPassword = ConfigurationHolder.config.ftp.password
String ftpHost = ConfigurationHolder.config.ftp.host
log.debug "ftp> ${ftpUser}@${ftpHost}"
JakartaFtpWrapper ftp = new JakartaFtpWrapper();
ftp.connectAndLogin(ftpHost, ftpUser, ftpPassword)
ftp.setDataTimeout(1000*60*60*5)
log.debug "Welcome message[${ftp.getReplyString()}]"
log.debug "Current Directory[${ftp.printWorkingDirectory()}]";
log.debug "remote dir[${remoteDir}]"
ftp.makeDirectory(remoteDir)
ftp.cwd(remoteDir)
log.debug "uploading file path[${f}]..."
ftp.binary()
ftp.enterLocalPassiveMode()
def input = new FileInputStream(f.getAbsolutePath());
OutputStream output = ftp.storeFileStream(f.getName())
Util.copyStream(input, output);
output.flush()
input.close();
output.close();
ftp.logout();
ftp.disconnect();
}
If I remove the connect from the each, the images got corrupted! am I doing something wrong here?
EDIT**: This one DOES NOT work:
String ftpUser = ConfigurationHolder.config.malibu.ftp.user
String ftpPassword = ConfigurationHolder.config.malibu.ftp.password
String ftpHost = ConfigurationHolder.config.malibu.ftp.host
log.debug "ftp> ${ftpUser}@${ftpHost}"
JakartaFtpWrapper ftp = new JakartaFtpWrapper();
ftp.connectAndLogin(ftpHost, ftpUser, ftpPassword)
ftp.setDataTimeout(1000*60*60*5)
log.debug "Welcome message[${ftp.getReplyString()}]"
log.debug "Current Directory[${ftp.printWorkingDirectory()}]";
log.debug "remote dir[${remoteDir}]"
ftp.makeDirectory(remoteDir)
ftp.cwd(remoteDir)
filesList.each{ f->
log.debug "uploading file path[${f}]..."
ftp.binary()
ftp.enterLocalPassiveMode()
def input = new FileInputStream(f.getAbsolutePath());
OutputStream output = ftp.storeFileStream(f.getName())
Util.copyStream(input, output);
output.flush()
input.close();
output.close();
}
ftp.logout();
ftp.disconnect();
EDIT :
link for JakartaFtpWrapper : http://www.nsftools.com/tips/JakartaFtpWrapper.java
EDIT 2 :
I`ve already tried adding the ftp.binary() inside, the loop, or outsite. both doesnt work.
One interesting think about it, is, that always the LAST photo got right in the ftp server, it seems that the first ones are more corrupted then the last ones as well!