java.net.ConnectException:连接被拒绝:检索文件时连接

时间:2017-05-06 15:24:10

标签: java ftp connection connect

我一直在寻找年龄,试图找出导致此错误的原因:

java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:920)
at org.apache.commons.net.ftp.FTPClient._retrieveFileStream(FTPClient.java:1980)
at org.apache.commons.net.ftp.FTPClient.retrieveFileStream(FTPClient.java:1967)
at me.ItsJasonn.Main.Core.init(Core.java:48)
at me.ItsJasonn.Main.Core$1.run(Core.java:21)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

大约几分钟前,我意识到它与文件或文件夹的权限有关(不确定是哪一个)。

我正在连接到位于我家的NAS FTP服务器并使用本地互联网。 这是我的代码:

package me.ItsJasonn.Main;

import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;

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

public class Core {
    private static ServerThread serverThread;

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                Core core = new Core();
                core.init();
            }
        });
    }

    public void init() {
        new MainInstance(this);

        BufferedReader reader = null;
        String ip = null;

        Socket socket = null;
            try {
                FTPClient client = new FTPClient();
                try {
                    client.connect("84.27.126.40", 21);
                    client.login(Login.USERNAME, Login.PASSWORD);

                    client.enterLocalPassiveMode();
                    client.setFileTransferMode(FTP.ASCII_FILE_TYPE);
                    client.setFileType(FTP.ASCII_FILE_TYPE);

                    client.changeWorkingDirectory("homes");
                    client.changeWorkingDirectory("Jason");

                    System.out.println(client.printWorkingDirectory());

                    InputStream stream = client.retrieveFileStream("kingkongproperties.txt");
                    reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
                    ip = reader.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if(reader != null) {
                        reader.close();
                    }

                    if(client.isConnected()) {
                        try {
                            client.logout();
                            client.disconnect();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

                socket = new Socket(InetAddress.getByName(ip), 4321);

                serverThread = new ServerThread(socket);
                Thread start = new Thread(serverThread);
                start.start();
            } catch (IOException e) {
                try {
                    System.out.println("No server found. Trying again in 2 seconds...");

                    Thread.sleep(2000);
                    init();
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
    }

    public ServerThread getServerThread() {
        return serverThread;
    }
}

1 个答案:

答案 0 :(得分:0)

您尝试连接的IP是路由器的IP。您需要将端口设置为nas,或者本地连接到nas地址。