Java 传输文件时发生FTP错误

Java 传输文件时发生FTP错误,java,ftp,Java,Ftp,我试图在java中运行FTP的以下代码,但遇到了错误 请帮我确认一下 public class FTPUploadFileDemo { public static void main(String[] args) { String server = "localhost"; int port = 21; String user = "username"; String pass = "password"; FTPClient ftpClient =

我试图在java中运行FTP的以下代码,但遇到了错误 请帮我确认一下

public class FTPUploadFileDemo {

public static void main(String[] args) {

    String server = "localhost";
    int port = 21;
    String user = "username";
    String pass = "password";

    FTPClient ftpClient = new FTPClient();
    try {

        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        // APPROACH #1: uploads first file using an InputStream
        File firstLocalFile = new File("C:/Users/un/workspace/Test.txt");

        String firstRemoteFile = "t1.txt";
        InputStream inputStream = new FileInputStream(firstLocalFile);

        System.out.println("Start uploading first file");
        boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
        inputStream.close();
        if (done) {
            System.out.println("The first file is uploaded successfully.");
        }


        File secondLocalFile = new File("C:/Users/un/workspace/Test.txt");
        String secondRemoteFile = "t2.txt";
        inputStream = new FileInputStream(secondLocalFile);


        OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
        byte[] bytesIn = new byte[4096];
        int read = 0;

        while ((read = inputStream.read(bytesIn)) != -1) {
            outputStream.write(bytesIn, 0, read);
        }
        inputStream.close();
        outputStream.close();

        boolean completed = ftpClient.completePendingCommand();
        if (completed) {
            System.out.println("The second file is uploaded successfully.");
        }

    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}}
上述程序显示以下错误:

错误:连接被拒绝:连接 java.net.ConnectException:连接被拒绝:连接


如果您在ftpClient.connect(服务器、端口)上收到此错误消息,请指导我如何解决此问题这意味着您的ftp服务器配置不正确,不接受本地连接;您的FTP服务器是本地的还是本例中的?

我的FTP服务器是本地的,我正在尝试首先在系统中本地传输它,这就是我编写localhost的原因,因此您应该首先测试FTP服务器配置。尝试使用FTP客户端(比如Filezilla,或者如果OS外壳包含FTP协议,则使用事件处理程序)连接到它,以便对其进行测试。如果无法工作,则必须修改FTP服务器配置以允许本地连接(或至少连接)。为了测试,你应该指定非常简单的用户名和密码来连接到你的FTP服务器(“你在第一篇文章中提到的用户名”和“密码”可能是个好主意,如果你给了我们假凭证)谢谢你的回复,但我已经试过了这个,filezilla,我提供了主机名作为“localhost”和我帐户的用户名密码,但它给出了以下错误:连接尝试失败,出现“ECONREFUNCE-服务器拒绝连接”,正在尝试下一个添加请告诉如何配置..这取决于您用作FTP服务器的软件。在Windows还是Linux下?什么套房?