Error on uploading file in java to ftp server

Karuvägistaja picture Karuvägistaja · Apr 23, 2012 · Viewed 8.7k times · Source

When I try to upload files to ftp server, I get errors. The server is a remote server but it offers ftp access. This is the code I am using

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fileuploaddemo;

import org.apache.commons.net.ftp.FTPClient;
import java.io.FileInputStream;
import java.io.IOException;

public class FileUploadDemo {
public static void main(String[] args) {
    FTPClient client = new FTPClient();
    FileInputStream fis = null;

    try {
        client.connect("ftp.adress.comlu.com");
        client.login("username", "mypass");

        //
        // Create an InputStream of the file to be uploaded
        //
        String filename = "D:/xxx/screen0.png";
        fis = new FileInputStream(filename);

        //
        // Store file to server
        //
        client.storeFile(filename, fis);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

This is the program output. Nothing gets uploaded and all I get are these lines.

run:
java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
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:479)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:552)
at org.apache.commons.net.ftp.FTP.port(FTP.java:877)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:709)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:565)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:557)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1795)
at fileuploaddemo.FileUploadDemo.main(FileUploadDemo.java:29)
BUILD SUCCESSFUL (total time: 1 second)

What could be the problem?

Answer

Kalpak Gadre picture Kalpak Gadre · Apr 23, 2012

Try,

client.enterLocalPassiveMode()

before you initiate transfer. I suspect you need to get into PASV mode for data transfer. This can happen in case you are behind a router (NAT) or a firewall. The FTP server will try to connect to you for data connection by default, which means you start listening on some port and invite server to connect to it for transfer of data. But in case you are behind firewall or router, the server cannot connect to you directly. PASV mode instructs FTP server to instead open a port on server for transfer and the client connects to it.