I'm trying to login to an ftp from Spring, but as much as I try I get the error:
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
import java.io.IOException;
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@PostMapping("/upload") public void uploadFile(
@RequestParam(name = "file", required = true) MultipartFile multipartFile) throws Exception{
//Map<String, Object> response = new HashMap<>();
final String host = env.getProperty("ftp.host");
final String user = env.getProperty("ftp.user");
final String password = env.getProperty("ftp.password");
FTPClient ftpClient = new FTPClient();
if(multipartFile == null || multipartFile.isEmpty()) {
throw new Exception("Elige un archivo válido");
}
try {
ftpClient.connect(host);
int connectionReply = ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(connectionReply)) {
System.out.println("No se ha podido conectar. El servidor FTP respondió con el código " + connectionReply);
}
boolean loginOk = ftpClient.login(user, password);
if(loginOk) {
System.out.println("Login correcto");
}else {
System.out.println("No se ha podido hacer login en el servidor FTP");
}
FTPFile[] files = ftpClient.listFiles();
for(FTPFile file : files) {
System.out.println(file.getName());
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
} catch(IOException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have done a println of the credentials and they are what they should be. I have tested them in FileZilla and it connects without problems.
For what it's worth, the error jumps on the line:
boolean loginOk = ftpClient.login(user, password);
...but I've searched the internet and can't find a cause for this case.
All the best!
After a lot of trying and trying, even with other libraries, I have returned to FTPClient and I have managed to solve the problem.
The first thing, which led me to the answer, was to add this line:
In this way, I can see in the console more detailed messages of what is happening, as I would see in the console of an ftp client like FileZilla, for example.
Thanks to this, I have seen the following error:
And as a result of that and with a little search I have found out that the solution was to replace...
by...
Hopefully this will be of use to someone at some point, because in my case it has been a lot of impotence for not understanding anything.