简体   繁体   中英

File not uploading successfully to FTP

I have written a stand alone Program to upload file to FTP Server. Code runs fine but I cannot find the file at FTP. Here is the code

import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;


public class FTPDemo {

    public static void main(String[] args) {

        FTPClient ftp = new FTPClient();
        int reply;

        try {

            ftp.connect("ip address");
            ftp.login("username","password");
            reply = ftp.getReplyCode();

            if(FTPReply.isPositiveCompletion(reply)){
                System.out.println("Connected Success");
            }else {
                System.out.println("Connection Failed");
                ftp.disconnect();
            }

            FileInputStream fis = null;
            String filename = "demo.txt";
            fis = new FileInputStream("C:\\demo.txt");
            System.out.println("Is file stored: "+ftp.storeFile(filename,fis));
            fis.close();
            ftp.disconnect();
        } catch (SocketException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
}

Is file stored returns false. What could be the problem ?

Let me quote you the FTPClient documentation :

The convention for all the FTP command methods in FTPClient is such that they either return a boolean value or some other value. The boolean methods return true on a successful completion reply from the FTP server and false on a reply resulting in an error condition or failure. The methods returning a value other than boolean return a value containing the higher level data produced by the FTP command, or null if a reply resulted in an error condition or failure. If you want to access the exact FTP reply code causing a success or failure, you must call getReplyCode after a success or failure.

In other words, to understand the actual reason for failure you need to call ftp.getReplyCode() and work from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM