Answer the question
In order to leave comments, you need to log in
Java Apache FPTS Client. How to defeat 425 Can't open data connection on storeFile()?
Good day!
I'm trying to upload a file to a remote FTP using Apache FTPSClient:
InputStream inputStream = new FileInputStream("newFile.txt");
FTPSClient ftpsClient = new FTPSClient();
try {
ftpsClient.connect("hostname");
ftpsClient.login("username", "password");
int reply = ftpsClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpsClient.disconnect();
}
boolean changed = ftpsClient.changeWorkingDirectory("files/");
if (!changed) {
ftpsClient.disconnect();
}
ftpsClient.setFileTransferMode(ftpsClient.BINARY_FILE_TYPE);
ftpsClient.enterLocalPassiveMode();
boolean stored = ftpsClient.storeFile("newFile.txt", inputStream);
if (stored) {
System.out.println();
}
ftpsClient.logout();
ftpsClient.disconnect();
} catch (Exception e) {
System.out.println(e.getMessage());
}
Answer the question
In order to leave comments, you need to log in
Good afternoon.
And tell me please, because it looks like you are trying to connect via FTP, but you are using FTPSClient
https://commons.apache.org/proper/commons-net/apid...
Maybe you need an FTPClient?
https://commons.apache.org/proper/commons-net/apid...
I can assume that you are trying to connect to FTPS using the port for FTP (21) and, accordingly, an error occurs
. For example, when connecting to FTP, I'm ok:
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import java.awt.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {
public static void main(String[] args) throws IOException {
FTPClient ftp = new FTPClient();
FTPClientConfig config = new FTPClientConfig();
ftp.configure(config);
boolean error = false;
try {
int reply;
String host = "example.com";
ftp.connect(host);
ftp.login("example_user", "12345");
System.out.println("Connected to " + host + ".");
System.out.print(ftp.getReplyString());
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
boolean changed = ftp.changeWorkingDirectory("public_html/");
if (!changed) {
ftp.disconnect();
}
ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
InputStream inputStream = new FileInputStream("/home/admin/Desktop/newFile.txt");
boolean stored = ftp.storeFile("newFile.txt", inputStream);
if (stored) {
System.out.println();
}
ftp.logout();
} catch(IOException e) {
error = true;
e.printStackTrace();
} finally {
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(IOException ioe) {
// do nothing
}
}
System.exit(error ? 1 : 0);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question