FTP error while downloading/uploading with ftp4j library in java application

4.9k Views Asked by At

I want to download a file in java application and when I try it, it creates the file on my hard drive but then fails to download it completely. I am using the ftp4j library to do it.

import it.sauronsoftware.ftp4j.*;
public class Main {
public static void main (String args[]){
    FTPClient client = new FTPClient();
    try{
    client.connect("ftp.myaddress.comlu.com");
    client.login("username", "password");
    System.out.println("Connection created");
    client.download("public_html/ZScreen.png", new java.io.File("d:/xxx/ZScreen.png"));
    System.out.println("Download successful");
    client.disconnect(true);
    }
    catch (Exception FTPException){
    System.out.println("Shit hit the fan");
    }
}
}

I always get the Connection created and Shit hit the fan. Also, there is a file created on my hard drive but it's size is 0 bytes.

This is the stack race

Connection created
java.net.SocketException: Connection reset
Shit hit the fan
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 sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:126)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:112)
at java.io.InputStreamReader.read(InputStreamReader.java:168)
at it.sauronsoftware.ftp4j.NVTASCIIReader.readLine(NVTASCIIReader.java:105)
at     it.sauronsoftware.ftp4j.FTPCommunicationChannel.read(FTPCommunicationChannel.java:142)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.readFTPReply(FTPCommunicationChannel.java:187)
at it.sauronsoftware.ftp4j.FTPClient.openPassiveDataTransferChannel(FTPClient.java:3538)
at it.sauronsoftware.ftp4j.FTPClient.openDataTransferChannel(FTPClient.java:3473)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3302)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3213)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3078)
at Main.main(Main.java:9)
2

There are 2 best solutions below

1
On

You do not have the rights to write on the folder. Check if the repertory is not under "read-only" state.

0
On

Apparently there is a bug on the Windows 7 firewall related to using FTP on IPv6 that would explain your problem. See bug report here.

Any one of the following workarounds should suffice to fix it:

  • Run the following as an administrator in a Windows console:

    netsh advfirewall set global StatefulFtp disable

  • Run the JVM with the option: -Djava.net.preferIPv4Stack=true