java中的被动ftp - 为什么流没有准备好

时间:2011-08-08 12:53:27

标签: java networking ftp

又一个java问题...... 有一个客户端应该通过被动模式连接到服务器。 它似乎工作正常,我得到IP地址和端口和passivesocket说它已经准备好了。

但是passivesocket.getInputStream根本没有准备好 - 所以我无法从中读取并且没有获得对LIST的响应。 无法弄清楚为什么,有什么建议吗?

public synchronized void getPasvCon() throws IOException, InterruptedException {

        // Commands abholen


        // IP Adresse holen
Thread.sleep(200);
        String pasv = commands.lastElement(); 
        String ipAndPort = pasv.substring(pasv.indexOf("(") + 1,
                pasv.indexOf(")"));

        StringTokenizer getIp = new StringTokenizer(ipAndPort);

        // holt die IP
        String ipNew = "";   // IP für den neuen Socket
        for (int i = 0; i < 4; i++) {
            if (i < 3) {
                ipNew += (getIp.nextToken(",") + ".");
            } else {
                ipNew += (getIp.nextToken(","));

            }
        }


        Integer portTemp1 = new Integer( getIp.nextToken(","));
        Integer portTemp2 = new Integer (getIp.nextToken(","));
        portNew = (portTemp1 << 8 )+ portTemp2;

System.out.println(">>>>> " + ipNew + ":" + portNew);       


        try {

            pasvSocket = new Socket(ipNew, portNew);
            System.out.println("Socket verbunden: "+  pasvSocket.isConnected());

        } catch (UnknownHostException e) {
            System.out.println("Host unbekannt");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            fromPasvServer = new BufferedReader(new InputStreamReader(
                    pasvSocket.getInputStream()));
            Thread.sleep(2000);
            System.out.println("Streams bereit: " + fromPasvServer.ready() + " | " );
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            writePasvCommands = new PrintWriter(pasvSocket.getOutputStream(),
                    true);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   


Thread.sleep(3000);
        Thread pasvKonsole = new Thread(new PasvKonsole(this));

        pasvKonsole.start();

    }

    public void LIST() throws IOException  {
    writeCommands.print("LIST\n");
    writeCommands.flush();

    }


    public void run() {
        try {
            connect();

    //      getStatus();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            USER();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        PASS();


        try {
            Thread.sleep(2000);
            PASV();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


import java.io.IOException;


public class PasvKonsole extends Thread {
    Client client;
    public PasvKonsole(Client client) {
        this.client = client;
    }

    public void run() {
        System.out.println("========= PasvKonsole started");

    while(true) {

        try {

            String lineP = client.fromPasvServer.readLine();
                System.out.println("***" + lineP);
                System.out.println("Ich bin da und tue auch was");  
        } catch (IOException e) {}

    }       
    }
}

1 个答案:

答案 0 :(得分:0)

添加了Thread.Sleep,它确实有效。