nomee.tec
nomee.tec

Reputation: 141

Ftp File download From java code

i have this code to download a single file .

    import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
public class NetTest {
public static void main(String[] args){
FTPClient client = new FTPClient( );
OutputStream outStream;
    try {

this is the part of server and passwords .

   client.connect( "servername" );
    client.login("noman123", "pass");
    String remoteFile = "/a.txt";
    outStream = new FileOutputStream( "a.txt" );

simple fill downloading but error on this line

    client.retrieveFile( remoteFile, outStream );
} catch(IOException ioe) {
    System.out.println( "Error communicating with FTP server." );
} finally {
    try {
        client.disconnect( );
    } catch (IOException e) {
        System.out.println( "Problem disconnecting from FTP server" );
    }
}

}
}

and it gives me errors like i hope that u can understand the issue that i m facing now

java.net.SocketException: Software caused connection abort: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109) at java.net.SocketOutputStream.write(SocketOutputStream.java:153) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221) at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291) at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:295) at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141) at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229) at java.io.BufferedWriter.flush(BufferedWriter.java:254) at org.apache.commons.net.ftp.FTP.__send(FTP.java:496) at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:470) at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:547) at org.apache.commons.net.ftp.FTP.port(FTP.java:872) at org.apache.commons.net.ftp.FTPClient.openDataConnection(FTPClient.java:667) at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:1595) at FtpDownloadDemo.main(FtpDownloadDemo.java:25)

Upvotes: 1

Views: 14392

Answers (4)

gaspa85
gaspa85

Reputation: 1

read this article https://bugs.java.com/bugdatabase/view_bug?bug_id=7077696

WORK AROUND to solve this problem start program with

-Djava.net.preferIPv4Stack=true

Upvotes: 0

The_Cute_Hedgehog
The_Cute_Hedgehog

Reputation: 1350

The documentation on the given example code that you should call the enterLocalPassiveMode method of FTPClient if you are behind a firewall.

Upvotes: 0

nomee.tec
nomee.tec

Reputation: 141

turn off the firewall.in this issue

Upvotes: 4

milletron
milletron

Reputation: 380

Make sure you can ping the server and log in manually, but assuming you're ok there, there's two additional things I would do.

1) Per the FTP Client documentation, check that you're really connected

// After connection attempt, you should check the reply code to verify success.
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
        { // print more complete error }

There's a full example here.

2) It could be since you're trying to get the remote file "/a.txt" you're trying to get to the root directory and your ftp server isn't set up to allow you access. Try just "a.txt" in whichever directory your ftp client is set to dump a user into.

Upvotes: 2

Related Questions