Apache Commons FTP storeFileStream returns 空

Apache Commons FTP storeFileStream returns null

我正在尝试使用 apache.commons.ftp 库将 android 中的文件上传到 FTP 服务器。

注意:我正在使用 storeFileStream 以便我可以跟踪上传进度。

ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
InputStream inputStream = new FileInputStream(localFile);
OutputStream outputStream = ftpClient.storeFileStream(remoteFile);
byte[] buffer = new byte[8192];
int bytesRead;
logMessage("Starting to upload file");

while ((bytesRead = inputStream.read(buffer)) != -1) {
    total += bytesRead;
    outputStream.write(buffer, 0, bytesRead); //output stream is null here
    latestPercentDone = (int) ((total / (float) totalMegaBytes) * 100);
    if (percentDone != latestPercentDone) {
        percentDone = latestPercentDone;
        publishProgress(""+percentDone);
    }
}
inputStream.close();
outputStream.close();
ftpClient.completePendingCommand();

我正在使用 speedtest.tele2.net 服务器进行测试。使用匿名登录。

日志一直说输出流为空。

编辑:使用 Martin Prikryl 并获取错误代码,我发现问题出在远程文件的位置。

上传到某个位置而不是目录。

例如,如果文件被命名为 item1.txt,那么远程文件应该是 /item1.txt/someFolder/AnotherFolder/item1.txt 而不是 /someFolder/AnotherFolder/

FTPClient.storeFileStream method可以returnnull:

An OutputStream through which the remote file can be written. If the data connection cannot be opened (e.g., the file does not exist), null is returned (in which case you may check the reply code to determine the exact reason for failure).

虽然“文件不存在”的提示对于上传来说是无稽之谈,但显然是.retrieveFileStream(下载)的复制粘贴错误。


使用.getReplyCode and .getReplyString,看看哪里出了问题。