I have an FTP server, I am trying to upload a file
in FTPClient.openDataConnection(), I get down to the line
if (!FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
return null;
}
The command is "STOR" and arg is "edf0864d-1651-43b2-8453-d160bf9d0742" (the name of the file I want to store)
The server log looks like:
PORT 86,185,26,230,213,101
200 Port command successful
STOR edf0864d-1651-43b2-8453-d160bf9d0742
150 Opening data channel for file upload to server of "/edf0864d-1651-43b2-8453-d160bf9d0742"
(Up to here after calling sendCommand)
PORT 86,185,26,230,213,102
200 Port command successful
From the logs the response code is 150, but sendCommand returns a response code of 200
Other potentially important information
I'm using filezilla server
The server works most of the time, with the exact same code. The initial upload of the file works fine, then when I choose another file to overwrite the first upload, this happens.
Following on from that, there's another file with the same name, but I haven't had trouble with that before.
The server is configured for passive connections as far as I can tell, it doesn't shout at me when I start it up like it used to, but the PASV command produces a 421 could not create socket error. I'm using ACTIVE_LOCAL mode
I'm using apache commons net 3.6
Let me know if there's any more information that can help
This is the relevant parts of the java:
FTPClient ftp = new FTPClient();
String server = <server ip>;
int port = 21;
String username = <username>;
String password = <password>;
try{
ftp.connect(server, port);
}
catch(SocketException e){
throw new FTPCouldNotConnectException("Threw SocketException during connection", e);
}
catch(IOException e){
throw new FTPCouldNotConnectException("Threw IOException during connection", e);
}
int reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){
try{
ftp.disconnect();
throw new FTPCouldNotConnectException("Reply Code: " + reply + ", Could not connect");
}
catch(IOException e){
throw new FTPCouldNotConnectException(
"Threw IOException during disconnection after failing to connect with reply code: " + reply,
e);
}
}
else{
try{
if(ftp.login(username, password)){
LOGGER.info("Successfully logged in to ftp server");
ftp.setFileTransferMode(org.apache.commons.net.ftp.FTP.STREAM_TRANSFER_MODE);
ftp.setFileStructure(org.apache.commons.net.ftp.FTP.FILE_STRUCTURE);
ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
ftp.enterLocalActiveMode();
}
else{
LOGGER.warn("Failed to log in to ftp server");
}
}
catch(IOException e){
throw new FTPCouldNotConnectException("Failed to log in to server, threw ioexception", e);
}
}
try{
ftp.storeFile("edf0864d-1651-43b2-8453-d160bf9d0742", new FileInputStream("test.jpg"));
}
catch(IOException e){
LOGGER.error("java.io.IOException caught", e);
}
I found the answer based on the fact that the first call of the code worked fine, but the second failed.
In the java code, I hadn't called FTPClient.completePendingCommand(), which as The docs say, causes subsequent calls to behave unexpectedly.