FTP to 1and1.com

2019-08-05 20:55发布

问题:

The code below works to two other sites I've tried, but will not work with my domain hosted by 1and1. The return code is always 500 - Permanent Negative Completion reply.

I know I'm connecting because FTPReply.isPositiveCompletion(reply) returns true. I tried it with both reading file off phone storage and sending it from a byte array populated early in the code. This is the byte array. Both return 500. Both work on the other sites.

If I don't use enterLocalPassiveMode() the code stops executing on the storeFile call. No exception, no socket time-out. It just ends there and the async task will not call again in that session. The code does not do that on the other sites.

I've tried both ASCII and BINARY file types. Both return 500. 1and1 site says to use my domain and port 21. I can connect with CoreFTP and read and write using both of the accounts I've set up.

I also tired ftp4j and had the same response with all scenarios so went back to Apache because the code was already written with robust error trapping.

I've tried both mydomain.com and ftp.mydomian.com. 500 on both. I also tried the dot quad I can see in the CoreFTP window, but i get "cannot resolve host name" with the Apache Java code. Maybe not a static IP?

This is what CoreFTP does. It connects on port 21 and then goes in to passive mode and an ASCII data connection.

It's a long shot, but has anyone else ever FTPed to their 1and1 domain using Java in Android Studio?

Greg

Resolving mydomain.com...
Connect socket #5684 to xx.xx.xx.xxx, port 21... 220 Microsoft FTP Service
USER ftp79815757-0
331 Password required for ftp79815757-0.
PASS **********
230 User logged in.
SYST
215 Windows_NT
Keep alive off... PWD
257 "/ftp79815757-0" is current directory.
PASV
227 Entering Passive Mode (xx,xxx,xx,xxx,xxx,xxx).
LIST
Connect socket #5700 to xx.xx.xx.xx, port 62894... 150 Opening ASCII mode data connection.
226 Transfer complete.
Transferred 51 bytes in 0.094 seconds

FTPClient mFtpClient = new FTPClient();
String ip = "my domain dot com";
String userName = "ftp79815757-0";
String pass = "password";

mFtpClient.connect(InetAddress.getByName(ip));
mFtpClient.login(userName, pass);
int reply = mFtpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
    mFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
    //one thread said this would do the trick
    mFtpClient.enterLocalPassiveMode();
    mFtpClient.enterRemotePassiveMode();

    InputStream stream = new ByteArrayInputStream(imageData);

    //I have two accounts. One points to images_in
    /*if (!mFtpClient.changeWorkingDirectory("images_in")) {
       Log.e("ChangeDir", String.valueOf(mFtpClient.getReplyCode()));
    }*/

    if (!mFtpClient.storeFile("remoteName.jpg", stream)) {
        Log.e("FTPUpload", String.valueOf(mFtpClient.getReplyCode()));
    }

    stream.close();
    mFtpClient.disconnect();
}

回答1:

Finally got it. The main problem was that I was using an old version of the Apache library. The Jar I was using was commons-net-1.4.jar. Someone in another thread pointed me to commons-net-3.3.jar.

I commented out both mFtpClient.enterLocalPassiveMode() and mFtpClient.enterRemotePassiveMode(), and with some trail and error it worked with FTP.BINARY_FILE_TYPE and not ASCII_FILE_TYPE. ASCII got the file there, but it was garbage.



标签: java android ftp