Apache Commons FTP storeFileStream返回null [英] Apache Commons FTP storeFileStream returns 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 使用 Use the 这篇关于Apache Commons FTP storeFileStream返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!.retrieveFileStream
(download)..getReplyString
,看看出了什么问题..getReplyCode
and .getReplyString
, to see what went wrong.