I am working on a project which will later upload a few files to an FTP server after they are modified...I have everything but uploading the file figured out. I can successfully connect to the FTP server, but once the file goes to upload, the program hangs for a couple minutes, then it states that it timed out.
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:762)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:565)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:557)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1795)
at AdvertisementCreator.main(AdvertisementCreator.java:128)
Here is the code I have for the FTP connection: (Keep in mind I omitted the login details)
FTPClient fClient = new FTPClient();
try {
fClient.connect(server, port);
showServerReply(fClient);
int replyCode = fClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("Operation failed. Server reply code: " + replyCode);
return;
}
boolean success = fClient.login(user, pass);
showServerReply(fClient);
if (!success) {
System.out.println("Could not login to the server");
} else {
System.out.println("You are now logged on!");
loginLoop = false;
}
fClient.enterLocalPassiveMode();
fClient.setFileType(FTP.BINARY_FILE_TYPE);
File localFile = new File("files\\shared.txt");
String remoteFile = "shared.txt";
InputStream inputStream = new FileInputStream(localFile);
System.out.println("Start uploading the file");
boolean done = fClient.storeFile(remoteFile, inputStream);
inputStream.close();
if (done) {
System.out.println(remoteFile+" has been uploaded successfully");
}
} catch (IOException ex) {
System.out.println("Oops! Something wrong happened");
ex.printStackTrace();
} finally {
try {
if (fClient.isConnected()) {
fClient.logout();
fClient.disconnect();
System.out.println("FTP Disconnected");
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
I have never really messed with Apache Commons FTP until today. If anyone could provide some insight, I would greatly appreciate it
Edit: I forgot to mention that before adding the following line, the file transferred, but when I tried to open it on the server, it was empty.
fClient.enterLocalPassiveMode();
fClient.setFileType(FTP.BINARY_FILE_TYPE);