10

I'm trying to upload a simple txt file via FTP using XAMPP and FileZilla.
I'm using the Apache Commons Net 3.0.1 Library.
This is my code, very basic things:

FTPClient client = new FTPClient();
InputStream in = new ByteArrayInputStream("IT WORKS! :D".getBytes());

try {
    client.connect("localhost");
    client.login("user", "password");
    client.enterLocalPassiveMode();
    client.storeFile("textfile.txt", in);
} finally {
    try {
        in.close();
        client.logout();
        client.disconnect();
    } catch (Exception e) {
    }
}


But... storeFile() throws a java.net.SocketException:

Exception in thread "main" java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:189)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.BufferedReader.fill(BufferedReader.java:154)
    at java.io.BufferedReader.read(BufferedReader.java:175)
    at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:58)
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:310)
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:290)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:474)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:547)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:596)
    at org.apache.commons.net.ftp.FTP.pasv(FTP.java:945)
    at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:719)
    at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:551)
    at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1704)
    at ftpexample.ftpexample.main(ftpprova.java:17)


What's the problem?? :( I tried also on an online hosting service, with the same result...
I wonder if this is a firewall or windows' services related problem??

skaffman
  • 398,947
  • 96
  • 818
  • 769
Oneiros
  • 4,328
  • 6
  • 40
  • 69
  • in my case the same error message was caused by incorrect configuration of VSFTPD: http://serverfault.com/questions/524695/vsftpd-drops-connection-on-pasv-command-with-aws – yegor256 Jul 19 '13 at 11:58

3 Answers3

17

Solved by running this as administrator in the command prompt:

netsh advfirewall set global StatefulFTP disable

This is a Java 7 bug on Windows machines, it is reported here.

Oneiros
  • 4,328
  • 6
  • 40
  • 69
  • 1
    I wish I had come across this answer 4 hours ago, I would have saved myself some frustration. This is exactly the same problem I was having, and this fix worked like a charm. Thank you! – Greg Case Mar 16 '12 at 16:09
2

Set:

client.setUseEPSVwithIPv4( true );

This works if you can't make changes to Window's firewall settings.

0

I'm honestly not sure but you should try the following:

Use something like the following code:

System.out.println(client.getReplyCode());
for(String s : client.getReplyStrings())
    System.out.println(s);

after client.login("user", "password"); to verify the status of your connection.

If you don't get any good hints from the code above, after invoking client.storeFile("textfile.txt", in); try to add client.completePendingCommand();.

Good luck! :)

Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154
  • Thank you for the answer :) i get "230 230 Logged on" by printing the replies and the same `SocketException` by invoking `client.completePendingCommand()` – Oneiros Nov 30 '11 at 22:47
  • 230 is good. I double checked and the `completePendingCommand()` is necessary only if you use the `storeFileStream` method (that you might want to try as an alternative). What line of code is throwing the exception? Is there any file created in the server (maybe empty)? – Marsellus Wallace Dec 01 '11 at 15:58