Java FTP LIST命令

时间:2015-11-08 19:27:49

标签: java ftp

您好我正在为uni项目制作FTP客户端,在开始之前我正在尝试使用FTP协议的基本命令。到目前为止,我已经能够成功登录到FTP服务器。现在,我想要做的是列出根目录以获取它的内容。

我已经实现了这种方法来完成我所描述的内容,但我似乎无法使其发挥作用。据我所知它确实进入被动模式,但是当我发出list命令时没有任何反应。我不明白为什么会这样。有人可以帮忙吗?

以下是相关代码:

public synchronized boolean list() throws IOException{
        sendLine("PASV");
        String response = readLine();
        if(!response.startsWith("227 "))
            throw new IOException("Could not request PASSIVE mode: " + response);

        String ip = null;
        int port = -1;
        int opening = response.indexOf('(');
        int closing = response.indexOf(')', opening + 1);

        if(closing > 0){
            String dataLink = response.substring(opening + 1, closing);
            StringTokenizer tokenizer = new StringTokenizer(dataLink, ",");
            try{
                ip = tokenizer.nextToken() + "." + tokenizer.nextToken() + "." + tokenizer.nextToken() + "." + tokenizer.nextToken();
                port = Integer.parseInt(tokenizer.nextToken()) * 256 + Integer.parseInt(tokenizer.nextToken());
            }catch(Exception e){
                throw new IOException("Received bad data information: " + response);
            }        
        }


        sendLine("LIST");
        response = readLine();
        return (response.startsWith("200 "));
    }

0 个答案:

没有答案