gpt4 book ai didi

java - 从 FTP 服务器读取文件,但 InputStream 始终为空

转载 作者:行者123 更新时间:2023-11-30 02:48:48 27 4
gpt4 key购买 nike

我想读取目录中的文件。我要添加: List<String> nomi = new ArrayList<String>();文件 Nomi.txt 的线串。

通过调试,我可以正确查看链接(001.jpg 002.jpg 003.jpg)和 ft(Nomi.txt)中的文件,但在流中我始终为空;

InputStream stream = f.retrieveFileStream(/*url_ftp + "/photo/"+ft*/ft);

我的完整代码是这样的:

private static abstract class GetLinksTask extends AsyncTask<String, Void, List<String>> {

protected List<String> doInBackground(String... urls) {
List<String> links = new ArrayList<String>();
String ft=null;
BufferedReader reader = null;
List<String> nomi = new ArrayList<String>();
FTPClient f = new FTPClient();

try {
int reply;
f.connect(url_ftp);
f.login(username,password );
reply = f.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
f.disconnect();
System.err.println("FTP server refused connection.");
}
FTPListParseEngine engine = f.initiateListParsing("photo");

while (engine.hasNext()) {
FTPFile[] files = engine.getNext(25); // "page size" you want
//FTPFile[] files = engine.getFiles(filter);
for (FTPFile file : files) {
if(file.getName().substring(file.getName().length()-3,file.getName().length()).equals("jpg")){
System.out.println(file.getName());
links.add(file.getName());
}else{
ft=file.getName();
InputStream stream = f.retrieveFileStream(/*url_ftp + "/photo/"+ft*/ft);
reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
//nomi.add(reader.readLine());
}
System.out.println(file.getName());
}
//names=nomi;
}
}catch (IOException e) {
e.printStackTrace();
}finally {
if (reader != null)
try {
reader.close();
}catch (IOException logOrIgnore) {
}
}

return links;
}

protected abstract void postExecute(List<String> links);

protected void onPostExecute(List<String> lists) {
postExecute(lists);
}

}

一些提示?谢谢

最佳答案

仅仅创建一个Reader是不够的

reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));

并关闭它:

reader.close();

介于两者之间的某个地方,您实际上必须读取数据:

String line;
while( (line = reader.readLine()) != null ){
nomi.add( line );
}

关于java - 从 FTP 服务器读取文件,但 InputStream 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24571571/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com