0

这是从上一个问题(可能有点不清楚)中重新措辞的。

我想通过 FTP 从远程服务器下载文本文件,将文本文件的内容读入字符串,然后丢弃该文件。我不需要实际保存文件。

我正在使用 Apache Commons 库,所以我有:

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

谁能帮忙,而不是简单地将我重定向到一个有很多可能答案的页面?

4

3 回答 3

3

不会为您完成这项工作,但是一旦您建立了连接,您就可以调用retrieveFile 并将其传递给OutputStream。你可以google一下,找到剩下的……

 FTPClient ftp = new FTPClient();
 ...
 ByteArrayOutputStream myVar = new ByteArrayOutputStream();
 ftp.retrieveFile("remoteFileName.txt", myVar);

字节数组输出流

检索文件

于 2012-07-25T17:55:11.217 回答
2

通常我会留下评论询问'你试过什么?'。但现在我感觉更慷慨了:-)

干得好:

private void ftpDownload() {
    FTPClient ftp = null;
    try {
        ftp = new FTPClient();
        ftp.connect(mServer);

        try {
            int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                throw new Exception("Connect failed: " + ftp.getReplyString());
            }
            if (!ftp.login(mUser, mPassword)) {
                throw new Exception("Login failed: " + ftp.getReplyString());
            }
            try {
                ftp.enterLocalPassiveMode();
                if (!ftp.setFileType(FTP.BINARY_FILE_TYPE)) {
                    Log.e(TAG, "Setting binary file type failed.");
                }
                transferFile(ftp);
            } catch(Exception e) {
                handleThrowable(e);
            } finally {
                if (!ftp.logout()) {
                    Log.e(TAG, "Logout failed.");
                }
            }
        } catch(Exception e) {
            handleThrowable(e);
        } finally {
            ftp.disconnect();
        }
    } catch(Exception e) {
        handleThrowable(e);
    }
}

private void transferFile(FTPClient ftp) throws Exception {
    long fileSize = getFileSize(ftp, mFilePath);
    InputStream is = retrieveFileStream(ftp, mFilePath);
    downloadFile(is, buffer, fileSize);
    is.close();

    if (!ftp.completePendingCommand()) {
        throw new Exception("Pending command failed: " + ftp.getReplyString());
    }
}

private InputStream retrieveFileStream(FTPClient ftp, String filePath)
throws Exception {
    InputStream is = ftp.retrieveFileStream(filePath);
    int reply = ftp.getReplyCode();
    if (is == null
            || (!FTPReply.isPositivePreliminary(reply)
                    && !FTPReply.isPositiveCompletion(reply))) {
        throw new Exception(ftp.getReplyString());
    }
    return is;
}

private byte[] downloadFile(InputStream is, long fileSize)
throws Exception {
    byte[] buffer = new byte[fileSize];
    if (is.read(buffer, 0, buffer.length)) == -1) {
        return null;
    }
    return buffer; // <-- Here is your file's contents !!!
}

private long getFileSize(FTPClient ftp, String filePath) throws Exception {
    long fileSize = 0;
    FTPFile[] files = ftp.listFiles(filePath);
    if (files.length == 1 && files[0].isFile()) {
        fileSize = files[0].getSize();
    }
    Log.i(TAG, "File size = " + fileSize);
    return fileSize;
}
于 2012-07-25T17:04:51.790 回答
0

您可以跳过下载到本地文件系统部分并执行以下操作:

    FTPClient ftpClient = new FTPClient();
    try {
       ftpClient.connect(server, port);
       ftpClient.login(user, pass);
       ftpClient.enterLocalPassiveMode();
       ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

       InputStream inputStream = ftpClient.retrieveFileStream("/folder/file.dat");
       BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "Cp1252"));

       while(reader.ready()) {
           System.out.println(reader.readLine()); // Or whatever
       }
       inputStream.close();

    } catch (IOException ex) {
         ex.printStackTrace();
    } finally {
         try {
             if (ftpClient.isConnected()) {
                 ftpClient.logout();
                 ftpClient.disconnect();
             }
         } catch (IOException ex) {
               ex.printStackTrace();
         }
    }
于 2019-11-15T09:51:12.893 回答