Apache Commons FTP storeFileStream返回null [英] Apache Commons FTP storeFileStream returns null

查看:597
本文介绍了Apache Commons FTP storeFileStream返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to upload a file in android to an FTP server using the apache.commons.ftp library.

注意:我正在使用storeFileStream,以便可以跟踪上载进度.

Note: I am using storeFileStream so that I can track the progress of the upload.

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服务器进行测试.使用匿名登录.

I am using the speedtest.tele2.net server for testing. Using an anonymous login.

日志一直在说outputstream为null.

The log keeps saying the outputstream is null.

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

Using what Martin Prikryl and getting the error code, i found the problem to be the location of the remote file.

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

Upload to a location rather than a directory.

例如,如果文件被压缩为item1.txt,则远程文件应为/item1.txt/someFolder/AnotherFolder/item1.txt而不是/someFolder/AnotherFolder/

For example if the file is caled item1.txt the remote file should be /item1.txt or /someFolder/AnotherFolder/item1.txt rather than /someFolder/AnotherFolder/

推荐答案

一个OutputStream,通过它可以写入远程文件. 如果无法打开数据连接(例如,文件不存在),则返回null(在这种情况下,您可以检查回复代码以确定失败的确切原因).

尽管文件不存在";注意对于上载是无意义的,显然是来自

Though the "file does not exist" note is nonsense for upload, it's obviously a copy-and-paste error from the .retrieveFileStream (download).

使用

Use the .getReplyCode and .getReplyString, to see what went wrong.

这篇关于Apache Commons FTP storeFileStream返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆