11

如何读取存储在我的 ftp 服务器上的文件的文件大小?我不喜欢实现另一个库。

我正在使用 commons-io-2.4 和 commons-net-3.1

来自 talhakosen 的答案和代码(谢谢!)

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;
}
4

3 回答 3

7

您好,您可以使用下面的代码,

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-11-16T10:39:05.010 回答
0

如果服务器不允许客户端列出文件。你可以试试size cmd。有关大小 FTP cmd 语法,请参阅FTP RFC3659 扩展。您也可以使用以下函数示例。

private long getRemoteSize(FTPClient client, String remoteFilePath) throws Exception {
    client.sendCommand("SIZE", remoteFilePath);
    String[] reply = client.getReplyStrings();
    String[] response = reply[0].split(" ");
    if(client.getReplyCode() != 213){
      throw new Exception(String.format("ftp client size cmd response %d",client.getReplyCode()));
    }
    return Long.parseLong(response[1]);
  };
于 2020-05-06T04:05:05.937 回答
-2
private long getFileSize(FTPClient ftpClient, String fileName) throws Exception 
{
   long fileSize = 0;
   String fileName; 
   //down file name 
   FTPFile[] files = ftpClient.listFiles(); //ftp list
   for ( i = 0; i < files.length; i++)
   { //ftp connect forder in files              
      if (fileName == files[i].getName())
      { 
        fileSize = files[i].getSize();
        return fileSize;
      }
   }         
}         
于 2013-03-28T04:36:39.620 回答