-2

需要使用 FTP 从服务器下载文件,而不使用现有库和 3rd parity 解决方案。我设法连接并登录到服务器,传输类型模式(ASCII)和被动模式,所以我得到了端口号并打开了新的 ServerSocket(端口)。但是当我调用 RETR 文件名时,我的程序在 InputStream.readLine() 上阻塞(在读取服务器端口时,意味着服务器没有响应)在调用 RETR 命令之前有什么我忘了做的吗?

//PASV
outputStream.println("pasv");    

//227 Entering Passive Mode(a1,a2,a3,a4,p1,p2)
String response = inputStream.readLine();   

// port = p1*256 + p2
ServerSocket serverSocket = new ServerSocket(port);

//RETR fileName 
outputStream.println("retr "+ fileName);

//server no answer
String reply = inputStream.readLine()
4

2 回答 2

0

FTP PASV 命令不会在客户端打开套接字,IP 和端口从服务器返回给客户端,基本上告诉客户端“Ok connect to me on this IP and port”。请查看 RFC 959 以了解实施细节。用 JAVA 实现 FTP 客户端并不是一个简单的过程。

于 2013-04-16T18:47:08.533 回答
0
    public void download(String remoteFile) {


    FTPClient ftpClient = new FTPClient();

    try {
        ftpClient.connect(server, 22);
        ftpClient.login(ftpUser, ftpPassword);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        // APPROACH #1: using retrieveFile(String, OutputStream)
        File downloadFile1 = new File("D:/ftpdosyam.txt");
        OutputStream outputStream = new BufferedOutputStream(
                new FileOutputStream(downloadFile1));
        boolean success = ftpClient.retrieveFile(remoteFile, outputStream);
        outputStream.close();

        if (success) {
            System.out.println("File #1 has been downloaded successfully.");
        }
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
于 2013-08-16T08:24:58.753 回答