gpt4 book ai didi

java - ftpClient.retrieveFileStream(filepath) 返回 null

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:19:49 30 4
gpt4 key购买 nike

我有一个文件夹,它在 ftp 中有 3 个文件。对于第一个文件,bufferreader 正在成功读取。但是从第二个文件 InputStream 获取为 null。不知道是什么原因。

我的代码是

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.util.logging.Logger;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class ExtractFile{

public static void main(String args[]) {

// get an ftpClient object
FTPClient ftpClient = new FTPClient();
FileOutputStream fos = null;

try {
// pass directory path on server to connect
ftpClient.connect("XXXX");

// pass username and password, returned true if authentication is
// successful
boolean login = ftpClient.login("XXXX", "XXXXX");

if (login) {
System.out.println("Connection established...");

FTPFile[] files=ftpClient.listDirectories();
for(int i=0;i<files.length;i++){


FTPFile file=files[i];

BufferedReader reader = null;
String firstLine = null;

try {

String currentline=null;

listDirectory(ftpClient, ftpClient.printWorkingDirectory(), "", 0);


} finally {
if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
}



}


boolean logout = ftpClient.logout();
if (logout) {
System.out.println("Connection close...");
}
} else {
System.out.println("Connection fail...");
}

} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
static void listDirectory(FTPClient ftpClient, String parentDir,
String currentDir, int level) throws IOException {
String dirToList = parentDir;
if (!currentDir.equals("")) {
dirToList += "/" + currentDir;
}

FTPFile[] subFiles = ftpClient.listFiles(dirToList);

if (subFiles != null && subFiles.length > 0) {
for (FTPFile aFile : subFiles) {
String currentFileName = aFile.getName();

if (currentFileName.equals(".")
|| currentFileName.equals("..")) {

continue;
}
for (int i = 0; i < level; i++) {
System.out.print("\t");
}
if (aFile.isDirectory()) {
System.out.println("[" + currentFileName + "]");
listDirectory(ftpClient, dirToList, currentFileName, level + 1);
} else {
System.out.println(currentFileName);
String currentline=null;
BufferedReader reader = null;
String firstLine = null;

/*****here i am getting stream as null**********/
InputStream stream = ftpClient.retrieveFileStream(dirToList+"/"+currentFileName);



reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
String[] lines=new String[5];
int i=0;

//TODO

while((currentline=reader.readLine())!=null){
lines[i]=currentline;
i++;
System.out.println(".."+currentline);
}

}

}
}
}
}

为什么我从第二个文件中得到的 InputStream 为空??

最佳答案

我遇到了同样的问题,通过调用 completePendingCommand() 并验证传输确实成功解决了这个问题,从而完成了文件传输。

documentation 中所述

To finalize the file transfer you must call completePendingCommand and check its return value to verify success. If this is not done, subsequent commands may behave unexpectedly.

如果这不能解决问题,请使用 getReplyCode() 或 getReplyString() 或 getReplyStrings() 查看确切的问题是什么。

编辑:刚注意到这是一篇很旧的帖子。但希望它会对其他人有所帮助。

关于java - ftpClient.retrieveFileStream(filepath) 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37296399/

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